這是我試圖從獲得JSON的網址: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 cannot load >https://shopicruit.myshopify.com/admin/orders.json ?>page=1&access_token=c32313df0d0ef512ca64d5b336a0d7c6. No 'Access-Control->Allow-Origin' header is present on the requested resource. Origin 'null' >is therefore not allowed access.
我還試圖JSONP,但它看起來像它不支持JSONP。
任何幫助和見解將不勝感激!
如果站點未配置爲允許跨源請求,則必須在您控制的服務器上使用服務器端代理。在瀏覽器中,您無法通過JavaScript進行任何操作。 – Pointy
當@Pointy提到代理時,他的意思是https://help.shopify.com/api/tutorials/application-proxies –
@Pointy你有鏈接指向我可以遵循的教程嗎? – singard