Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Axios post bugs All In One #107

Open
xgqfrms opened this issue May 20, 2021 · 5 comments
Open

Axios post bugs All In One #107

xgqfrms opened this issue May 20, 2021 · 5 comments
Labels
Axios post json Axios post json 改写 Axios 改写 Axios

Comments

@xgqfrms
Copy link
Owner

xgqfrms commented May 20, 2021

Axios post bugs All In One

{} 括号的坑 ❌

import axios from '@/utils/http.js';


// 保存创意
const postCreativeSave = (options = {}) => {
    // post application/x-www-form-urlencoded ✅
    return axios.post('/api/creative/ks', options);
    // post multipart/form-data ❌
    // return axios.post('/api/creative/ks', {params});
};

image

application/x-www-form-urlencoded

multipart/form-data

application/json

@xgqfrms
Copy link
Owner Author

xgqfrms commented May 20, 2021

@xgqfrms
Copy link
Owner Author

xgqfrms commented May 20, 2021

solution

屏蔽细节

import axios from '@/utils/http.js';


// 保存创意
const axiosUtils = (url = '', params = {}, type = 'get') => {
    if(!url) {
        throw new Error('❌ API URL 不可为空!');
        // return;
    }
    // switch...case
    if(type === 'post') {
        // application/x-www-form-urlencoded ✅
        return axios.post(url, params);
    }
    if(type === 'get') {
        // multipart/form-data ✅
        return axios.post(url, {params});
    }
    if(type === 'put') {
        // multipart/form-data ✅
        return axios.post(url, {params});
    }
};

@xgqfrms xgqfrms changed the title Axios post All In One Axios post bugs All In One May 20, 2021
@xgqfrms xgqfrms added the Axios post json Axios post json label Sep 24, 2021
@xgqfrms
Copy link
Owner Author

xgqfrms commented Sep 24, 2021

@xgqfrms
Copy link
Owner Author

xgqfrms commented Sep 24, 2021

改写 Axios

let oldTimestamp = null;
// 每次请求都带上token
instance.interceptors.request.use(config => {
    // // todo 为了减少测试的报错信息,暂时过滤工作表下的所有拦截逻辑,但上线前必须去掉
    // const isTable = window.location.href.indexOf('/table-2') > -1;
    // if (!pendingUrlWhitelist.includes(config.url) && !isTable) {
    if (!pendingUrlWhitelist.includes(config.url)) {
        removePending(config);
        addPending(config);
    }

    config = requestWithToken(config);
    // 处理 post|delete|put 请求,以formdata形式传参
    if (['post', 'delete', 'put'].includes(config.method)) {
        for (let key in config.data) {
            if (config.data.hasOwnProperty(key) && config.data[key] === null || config.data[key] === undefined) {
                delete config.data[key];
            }
        }
       // 不传 header 默认, query string
        if (!config.headers.hasOwnProperty('Content-Type')) {
            config.data = qs.stringify(config.data);
            config.headers['Content-Type'] = 'application/x-www-form-urlencoded';
        } else {
           // post json
        }
    }
    oldTimestamp = performance.now();

    return config;
}, error => {
    oldTimestamp = performance.now();
    return Promise.reject(error);
});

@xgqfrms
Copy link
Owner Author

xgqfrms commented Sep 24, 2021

demos

axios post JSON

    // 更新模板
    updateColumn (data = {}) {
        return axios.put('/api/custom_column/column_preset', data, {
            headers: { 'Content-Type': 'application/json' },
        });
    },

axios post queryString

// 保存创意
const postCreativeSave = (options = {}) => {
    // post application/x-www-form-urlencoded
    return axios.post('/api/creative/kuaishou', options);
};

axios post formData

// 保存创意
const postCreativeSave = (options = {}) => {
    // post application/formdata
    return axios.post('/api/creative/kuaishou', {params});
};

@xgqfrms xgqfrms added the 改写 Axios 改写 Axios label Sep 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Axios post json Axios post json 改写 Axios 改写 Axios
Projects
None yet
Development

No branches or pull requests

1 participant