Cors Post请求中的Axios网络错误,状态代码为200

时间:2018-05-14 10:02:31

标签: http cors http-headers axios

我使用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

还有一个关于标题缺失的CORS警告

阻止跨源请求:同源策略禁止在 http://127.0.0.1:8080读取远程资源。 (原因:缺少CORS标题'Access-Control-Allow-Origin'。

但它包含在选项请求中!

options

当我在Axios请求标头中添加'Access-Control-Allow-Origin': '*'时,警告消失了,但是在成功的选项请求之后,浏览器不会触发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 //无差异

0 个答案:

没有答案
相关问题