我使用axios与我自己的API进行通信(不是用NodeJS编写的) .

当我发布非简单请求时,axios总是直接进入catch块,在控制台中显示网络错误,即使有2个成功的Http请求也是如此 .

Headers

requests

错误:网络错误堆栈跟踪:createError @ http://localhost:3000/static/js/bundle.js:1634:15 handleError @ http://localhost:3000/static/js/bundle.js:1170:14


还有一个关于丢失 Headers 的CORS警告

跨源请求已阻止:同源策略不允许在http://127.0.0.1:8080读取远程资源 . (原因:缺少CORS Headers 'Access-Control-Allow-Origin') .

但它包含在选项请求中!

options

当我在Axios请求标头中添加 'Access-Control-Allow-Origin': '*' 时,警告消失,但浏览器在成功的选项请求后不会触发Post请求 .

为了完成这里是post请求标头 .

post


代码:

postForm = () => {
    axios.post("http://127.0.0.1:8080/",
            myComplexObj, {
                headers: {
                    //'Access-Control-Allow-Origin': '*',
                    'Content-Type': 'application/json'
                },
                timeout: 15000
            }
        ).then(res => {
            console.log(res);
            alert('success');
        })

        .catch(function(error) {
            //code always end up here
            console.log(error);
            /*Error: Network Error
           Stack trace:
           createError@http://localhost:3000/static/js/bundle.js:1634:15
           handleError@http://localhost:3000/static/js/bundle.js:1170:14
           */

            console.log(error.response);      //undefined
            console.log(error.response.data); //undefined
            }
        })

任何帮助都很高兴 . 我试过的:

  • 删除超时//无更改

  • 删除Catch块//仍然没有成功

  • 在选项和/或发布请求上返回状态代码204 //没有区别