2016-03-22 13 views
0

我想使用基本驗證將API調用放入BigCommerce API(https://developer.bigcommerce.com/api)中。由於CORS無法驗證到BigCommerce API中

我的AJAX調用看起來如下:

$.ajax({ 
    type: 'GET', 
    url: 'https://store-zkk8z5i.mybigcommerce.com/api/v2/products', 
    xhrFields: { 
     withCredentials: true 
    }, 
    headers: { 
     'Authorization': 'Basic ' + MY_TOKEN, 
     'Accept': 'application/json' 
    }, 
    success: function (event) { 
     debugger 
    }, 
    error: function (error) { 
     debugger 
    } 
}); 

但我不斷收到以下錯誤:

XMLHttpRequest cannot load https://store-zkk8z5i.mybigcommerce.com/api/v2/products . Response to preflight request doesn't pass access control check: A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header when the credentials flag is true. Origin ' http://store-zkk8z5i.mybigcommerce.com ' is therefore not allowed access. The credentials mode of an XMLHttpRequest is controlled by the withCredentials attribute.

我敢肯定,我的資格沒關係,因爲我得到成功響應,當我使用cURL提出我的請求。

任何想法?

回答

1

I'm pretty sure that my credentials are okay

這很重要。看看錯誤消息:

A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header when the credentials flag is true.

既然你發送憑據,您不能使用通配符Access-Control-Allow-Origin

你必須說Access-Control-Allow-Origin: http://store-zkk8z5i.mybigcommerce.com而不是Access-Control-Allow-Origin: *

+0

要添加到這一點,我們不建議從店面做出請求直接通過API出於安全原因: https://stackoverflow.com/questions/35942220/is-bigcommerce-api-supports-cors – Alyss

+0

但我從來沒有將Access-Control-Allow-Origin標頭設置爲*任何地方。我認爲瀏覽器本身就是這樣做的。有沒有辦法阻止它?我試着按照你的建議設置,沒有區別 – satnam

+0

瀏覽器沒有這樣做,'store-zkk8z5i.mybigcommerce.com'是。它是一個HTTP響應頭。如果你不控制'store-zkk8z5i.mybigcommerce.com',那麼你需要(a)詢問任何人改變它或者(b)找到一些解釋如何在不設置'withCredentials的情況下使用他們的API的文檔:真正的......「,但考慮到Alyss的評論,這些看起來都不太可能,所以你應該切換到從你的服務器發出HTTP請求。 – Quentin