繁体   English   中英

POSTMAN工作时角度POST交叉原点错误

[英]Angular POST cross origin error while POSTMAN works

我尝试从我的角度登录服务POST:

$http.post('https://xyz/login',
            {
                headers: {
                    'Content-type': 'application/json',
                    'Accept': 'application/json',
                    'signature': 'asd'
                }

我收到这个错误:

XMLHttpRequest cannot load https://xyz/login. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:1337' is therefore not allowed access.

我试过这个标题:

$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];

还有这些:

"Access-Control-Allow-Origin": "*";
"Access-Control-Allow-Headers": "X-Requested-With";
"Access-Control-Allow-Methods": "GET, POST", "PUT", "DELETE";

有趣的是,POSTMAN是有效的。 我该怎么办?

谢谢。

您的请求包括非简单标头Content-typesignature ,必须包含在响应的Access-Control-Allow-Headers标头中。

Content-type有时是一个简单的标题,但仅适用于特定的值application/json不是其中一个值,它会导致Content-type变得非常简单。)

在服务器的预检响应中将Content-type添加到Access-Control-Allow-Headers

POSTMAN不受同源策略的约束,因此它不需要来自服务器的CORS支持。

您的浏览器是否在POST之前发出OPTIONS请求? 检查NET选项卡我在浏览器或Angular(不知道哪个)和服务器没有的OPTIONS请求之前遇到了问题...

“Access-Control-Allow-Methods”:“GET,POST”,“PUT”,“DELETE”,“OPTIONS”;

不确定您是否已经拥有所需的信息。 但是在我的本地网络服务器中 - 当我使用邮递员发出http请求时,它会添加到标题中:

headers:{host:'localhost',connection:'keep-alive',pragma:'no-cache','cache-control':'no-cache','user-agent':'Mozilla / 5.0(Windows NT) 10.0; WOW64)AppleWebKit / 537.36(KHTML,与Gecko一样)Chrome / 47.0.2526.106 Safari / 537.36',接受:' / ',referer:' http:// localhost / ','accept-encoding':'gzip,deflate ,sdch','accept-language':'en-US,en; q = 0.8'},

以下是我在rawHeaders中看到的内容:['Host','localhost','Connection','keep-alive','Pragma','no-cache','Cache-Control','no-cache' ,'用户代理','Mozilla / 5.0(Windows NT 10.0; WOW64)AppleWebKit / 537.36(KHTML,如Gecko)Chrome / 47.0.2526.106 Safari / 537.36','接受',' / ','Referer',' http:// localhost / ','Accept-Encoding','gzip,deflate,sdch','Accept-Language','en-US,en; q = 0.8'],

所以,也许你只需要假装你的客户端成为公认的浏览器客户端。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM