这是我正在尝试获取JSON的URL: https://shopicruit.myshopify.com/admin/orders.json?page=1&access_token=c32313df0d0ef512ca64d5b336a0d7c6
然而,我已经尝试了CORS,但是失败了。以下是我从模板中遵循的代码:
function createCORSRequest(method, url) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
// XHR for Chrome/Firefox/Opera/Safari.
xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined") {
// XDomainRequest for IE.
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
// CORS not supported.
xhr = null;
}
return xhr;
}
// Helper method to parse the title tag from the response.
function getTitle(text) {
return text.match('<title>(.*)?</title>')[1];
}
// Make the actual CORS request.
function makeCorsRequest() {
var url = 'https://shopicruit.myshopify.com/admin/orders.json?page=1&access_token=c32313df0d0ef512ca64d5b336a0d7c6';
var xhr = createCORSRequest('GET', url);
if (!xhr) {
alert('CORS not supported');
return;
}
// Response handlers.
xhr.onload = function () {
var text = xhr.responseText;
console.log("success");
};
xhr.onerror = function () {
alert('Woops, there was an error making the request.');
};
xhr.send();
}
makeCorsRequest();
我仍然收到这个错误:
XMLHttpRequest无法加载>https://shopicruit.myshopify.com/admin/orders.json?>page=1&access_token=c32313df0d0ef512ca64d5b336a0d7c6。所请求的资源上没有'Access-Control-Allow-Origin'头。因此,来自'null'的请求不被允许访问。
我还尝试了JSONP,但它似乎不支持JSONP。
任何帮助和见解都将不胜感激!