使用XMLHttpRequest时没有'Access-Control-Allow-Origin'

时间:2018-04-26 16:08:08

标签: xmlhttprequest

遵循Codeship API的文档:https://apidocs.codeship.com/v2/projects/update-project

我正在尝试使用XMLHttpRequest来获取数据。代码是从文档生成的,所以我假设它应该可以工作。

var data = "{}";

var xhr = new XMLHttpRequest();
xhr.withCredentials = true;


xhr.addEventListener("readystatechange", function () {
  if (this.readyState === this.DONE) {
    console.log(this.responseText);
  }
});

xhr.open("GET", "https://api.codeship.com/v2/organizations/uuidhere/projects?type=basic");
console.log('good');
xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
console.log('ok');
xhr.setRequestHeader("authorization", token here);

xhr.send(data);

console.log(data)

然而,当我在我的localhost中测试时,它给了我

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:3000' is therefore not allowed access. The response had HTTP status code 404.

在放

之后仍然会发生这种情况很奇怪

xhr.setRequestHeader('Access-Control-Allow-Origin', '*');

请帮忙

0 个答案:

没有答案