我知道这个问题之前已经被解决过很多次了。但我可以在响应中看到标题上已经有 Access-Control-Allow-Origin 。
这是我的请求代码:
var req = {
method: 'POST',
url: "integration-api.domain.com/login",
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'X-AN-WebService-IdentityKey': identitykey,
'X-AN-WebService-CustomerAuthToken': customerAuth,
'X-AN-WebService-CustomerTransientToken': transientToken,
'Cache-Control': 'no-cache'
},
data: datatopass
};
console.log(datatopass);
return $http(req).then(function(response){
console.log(response);
return response.data;
}, function(err) {
console.log(err);
return err;
});
这是在 chrome 上查看时的请求 header :
Accept:/
Accept-Encoding:gzip, deflate, sdch, br
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:x-an-webservice-identitykey
Access-Control-Request-Method:POST
Cache-Control:no-cache
Connection:keep-alive
Host:integration-api.domain.com
Origin:http://OriginDomain
Pragma:no-cache
Referer:http://OriginDomain/apitest/
User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36
那么预检响应是:
Access-Control-Allow-Headers:x-an-webservice-identitykey, origin, content-type, x-an-webservice-customerauthtoken, x-an-webservice-deviceauthtoken,x-an-webservice-customertransienttoken,x-an-webservice-version
Access-Control-Allow-Origin:*
Date:Wed, 30 Nov 2016 06:56:50 GMT
服务器是否需要在响应中添加 Access-Control-Allow-Method 或任何其他 header ,或者我是否需要在代码中添加某些内容?
我正在使用 angular.js 来调用对 url 的 $http 调用
最佳答案
您还需要允许方法类型
"Access-Control-Allow-Origin", "*";
'Access-Control-Allow-Methods', 'OPTIONS,GET,PUT,POST,DELETE";
"Access-Control-Allow-Headers", "X-Requested-With, Content-Type";
请参阅此答案。 Response to preflight request doesn't pass access control check in NodeJS
关于javascript - 添加 Access-Control-Allow-Origin : * 后仍然出现 "No ' Access Control Allow Origin' header is present on the requested resources”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40882393/