CORS“No'Access-Control-Allow-Origin'标头出现在请求的资源上。”

时间:2016-11-27 13:47:26

标签: angularjs cors

我在CORS上遇到错误:

  

“XMLHttpRequest无法加载http://graphql-swapi.parseapp.com/。对预检请求的响应未通过访问控制检查:请求的资源上没有”Access-Control-Allow-Origin“标头。来源”http://localhost:3000因此,不允许访问。响应的HTTP状态代码为405.“

调用服务方法时出错。

代码段:

let headers = new Headers();
headers.append('Access-Control-Allow-Origin', '*');
headers.append('Content-Type', 'application/json'); 
headers.append('Accept', 'application/json');
headers.append('Access-Control-Allow-Methods', 'POST');
headers.append('Access-Control-Allow-Headers', 'Content-Type');

//console.log(headers);

return this._http.post(apiUrl, { query: query, vars: '{}' }, { headers: headers })
  .map(res => res.json().data.planets);

当我在浏览器中禁用CORS时,我得到:

  

XMLHttpRequest无法加载xxx。预检的响应具有无效的HTTP状态代码405错误。

查询和代码肯定是正确的。

1 个答案:

答案 0 :(得分:0)

  

响应的HTTP状态代码为405

Look that up

  

405方法不允许

  

对预检请求的响应

Look that up

  

"预检"请求首先通过OPTIONS方法向另一个域上的资源发送HTTP请求,以确定实际请求是否可以安全发送

您的服务器端代码可能设置为将CORS标头添加到POST请求的响应中,但您还没有设置它来处理OPTIONS请求。