【发布时间】:2019-10-16 01:00:59
【问题描述】:
我想调用在用户系统中运行的第 3 方桌面应用程序,该应用程序的端口在 3000 处打开,当用户打开我的网站时,我向他的 localhost:3000 端口发出请求并获取数据,但是第 3 方应用程序没有为 CORS 设置响应标头,因此我无法使用此策略进行同步,有人知道这样做的方法吗? 在这里,我使用 ngrok 来隧道,但仍然无法做到。
xhr.open("POST", 'https://576b4cc1.ngrok.io', true);
//Send the proper header information along with the request
xhr.setRequestHeader("Content-Type", "text/plain");
xhr.onreadystatechange = function () { // Call a function when the state changes.
if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
// Request finished. Do processing here.
console.log("In ready state exchange: " + xhr.responseText);
resolve();
}
}
xhr.onload = function (data) {
console.log("In onload : " + data);
}
xhr.onerror = function (err) {
console.log("In error: " + JSON.stringify(err, ["message", "arguments", "type", "name"]));
console.log('Data incase : ' + xhr.statusText);
}
xhr.send(reqObj.data);
【问题讨论】:
-
CORS 要求是一种安全措施。没有变通方法。
-
在理想情况下,您将设置一个代理来处理正确的 CORS 标头,并让它将请求中继到第 3 方应用程序。如果第 3 方应用程序不打算通过添加 CORS 标头来使其应用程序更安全。
标签: javascript browser cors xmlhttprequest