我使用下面的代碼從shopify獲取客戶詳細信息。我已將我的域名重定向到shopify管理員的其他域。Shopify API跨域Ajax請求
function setEmailWithLoggedInUser(callback) {
$.ajax({
url: 'https://new-website-shopify.myshopify.com/admin/customers/'+__st.cid+'.json',
crossDomain: true,
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", "Basic XXXXXXXXXXXX")
}, success: function(data){
console.log(data);
if(callback)
callback();
}
})
我已經做了很多工作,但無法找到解決方案。 我收到此錯誤:
Failed to load resource: the server responded with a status of 404 (Not Found)
XMLHttpRequest cannot load https://new-website-shopify.myshopify.com/admin/customers/7094124372.json . Response to preflight request doesn't pass access control check:
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' https://www.beirutshopping.com ' is therefore not allowed access. The response had HTTP status code 404.
*「服務器響應狀態爲404(未找到)」*是根本問題。您看到的唯一原因*「否」Access-Control-Allow-Origin'標題出現在請求的資源上*。「是,大多數服務器不會將標題(如Access-Control-Allow-Origin標題)添加到4xx錯誤響應 - 相反,服務器通常只會將這些標題添加到2xx成功響應中。如果您將「https:// new-website-shopify.myshopify.com/admin/customers/7094124372.json」URL(無論真實的主機名是什麼)粘貼到瀏覽器地址欄中,會發生什麼情況? – sideshowbarker
如果我在瀏覽器中使用真實主機名粘貼「https://new-website-shopify.myshopify.com/admin/customers/70 94124372.json」,它將重定向回相同的域名,它會要求API憑證,然後正確顯示響應。 – user3326941
您可以控制'https:// new-website-shopify.myshopify.com/admin/customers /'?你能夠對其進行配置更改?它運行的是什麼服務器後端?您在這裏遇到的問題似乎與https://stackoverflow.com/questions/45557941/401-error-jwt-token-not-found-using-fetch/45559433#45559433中的解答相同。另請參閱*如何避免CORS預檢*部分答案在https://stackoverflow.com/questions/43871637/no-access-control-allow-origin-header-is-present-on-the-requested- resource-whe/43881141#43881141一些可能的方法來解決此問題。 – sideshowbarker