API违反了CORS政策

时间:2019-04-11 13:59:23

标签: javascript jquery api cors

我在使用API​​时遇到很多麻烦。我尝试了很多下面列出的事情。这是我的代码:

var form = new FormData();
form.append("relativeSource", " Mindful-Life.xhtml"); //space?
form.append("arguments", "-sp IXBRL,XBRL,Dimension,Formula -X");
form.append("input", " Mindful-Life.zip"); //space?

var settings = {
  async: true,
  crossDomain: true,
  url: "https://validation.cipc.co.za/bushchat-api/ws1000/ws1001",
  method: "POST",

  headers: {
    "cache-control": "no-cache",
    "postman-token": "1c3276ea-a939-7b3a-ee71-b6cfd2d3b1f7"
  },
  processData: false,
  contentType: false,

  mimeType: "multipart/form-data",
  data: form,
  error: error => {
    console.log(JSON.stringify(error));
  }
};

$.ajax(settings).done(function(response) {
  console.log(response);
});

如果运行此命令,则会出现以下错误:

  

从原点'null'对'https://validation.cipc.co.za/bushchat-api/ws1000/ws1001'处XMLHttpRequest的访问已被CORS策略阻止:对预检请求的响应未通过访问控制检查:没有'Access-Control-Allow-Origin'标头出现在请求的资源上。

因此,我尝试使用CORS教程网站中的代码,而以下是我使用的代码:

function createCORSRequest(method, url) {
  var xhr = new XMLHttpRequest();
  if ("withCredentials" in xhr) {
    xhr.open(method, url, true);
  } else if (typeof XDomainRequest != "undefined") {
requests.
    xhr = new XDomainRequest();
    xhr.open(method, url);
  } else {
    xhr = null;
  }
  return xhr;
}
var url = "https://validation.cipc.co.za/bushchat-api/ws1000/ws1001";
var xhr = createCORSRequest(
  "POST",
  "https://validation.cipc.co.za/bushchat-api/ws1000/ws1001"
);

if (!xhr) {
  throw new Error("CORS not supported");
}

xhr.onload = function() {
  var text = xhr.responseText;
  var title = getTitle(text);
  alert("Response from CORS request to " + url + ": " + title);
};

xhr.onerror = function() {
  alert("Woops, there was an error making the request.");
};

var form = new FormData();
form.append("relativeSource", " Mindful-Life.xhtml"); 
form.append("arguments", "-sp IXBRL,XBRL,Dimension,Formula -X");
form.append("input", " Mindful-Life.zip"); 

xhr.setRequestHeader("cache-control", "no-cache");
xhr.setRequestHeader("postman-token", "1c3276ea-a939-7b3a-ee71-b6cfd2d3b1f7");
xhr.send(form);

所有这些都符合CORS政策,但出现了完全相同的错误。

所以我将这个带有少量html的javascript托管到https主机,然后错误变为原点“ null”,错误变为原点“ https:// ...”,但仍然是相同的错误。

我可能会缺少什么?

0 个答案:

没有答案