javascript - 由于 CORS,无法对 BigCommerce API 进行身份验证

标签 javascript ajax api bigcommerce

我想使用基本身份验证对 BigCommerce API ( https://developer.bigcommerce.com/api ) 进行 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 发出请求时,我得到了成功的响应。

有什么想法吗?

最佳答案

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: *.

关于javascript - 由于 CORS,无法对 BigCommerce API 进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36167082/

相关文章:

javascript - Angular Material sidenav 指令在 app.component 中不起作用

javascript - 如何在angularjs中将ng-model的初始值设置为空字符串?

javascript - jquery $.post 解释

android - Retrofit - 解析没有 SerializedName 的 json

javascript - Vimeo javascript api player.play() 不播放

javascript - 有没有办法在 React 应用程序中链接非 React html 文件?

javascript - 禁用不适用于我的按钮

jQuery .getJSON 响应忽略 JSON 响应中的转义字符

java - 如何在不刷新页面的情况下将当前页面的url发送到servlet

适用于所有 API 级别的 Android 剪贴板代码