![](/img/trans.png)
[英]The post request works in Postman and in cURL but not in Angular
[英]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-type
和signature
,必须包含在响应的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.