【发布时间】:2017-04-11 07:28:55
【问题描述】:
我在使用 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 错误。
查询和代码肯定是正确的。
【问题讨论】: