【问题标题】:Still having "No 'Access Control Allow Origin' header is present on the requested resource" error after adding Access-Control-Allow-Origin: *添加 Access-Control-Allow-Origin 后,仍然出现“请求的资源上没有“访问控制允许来源”标头”错误:*
【发布时间】:2017-04-14 10:10:12
【问题描述】:

我知道这个问题之前已经解决过很多次了。但是我可以在响应中看到标题上已经有 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 上查看时的请求标头:

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 -方法: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:*
日期:格林威治标准时间 2016 年 11 月 30 日星期三 06:56:50

服务器是否需要在响应中添加 Access-Control-Allow-Method 或任何其他标头,或者我是否需要在我的代码中添加一些内容?

我正在使用 angular.js 调用对 url 的 $http 调用

【问题讨论】:

  • 你是如何解决这个问题的

标签: javascript angularjs web-services api cors


【解决方案1】:

你也需要允许方法类型

"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

【讨论】:

  • 非常感谢!那么服务器端团队也需要添加这些 CORS 选项吗?
  • 是的,如果要从不同的域调用 api。
  • 我有允许的方法,还是同样的问题
猜你喜欢
  • 2017-12-16
  • 1970-01-01
  • 2017-02-07
  • 2021-04-13
  • 2013-12-24
  • 2023-03-16
  • 2014-07-30
  • 2016-01-14
  • 2023-03-15
相关资源
最近更新 更多