【问题标题】:Bigcommerce API status code 401Bigcommerce API 状态码 401
【发布时间】:2017-12-15 20:27:32
【问题描述】:

我收到了错误

Failed to load https://store-cmr1f5oakh.mybigcommerce.com/api/v2/products: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://tim-yma5.mybigcommerce.com' is therefore not allowed access. The response had HTTP status code 401.

我用ajax调用api

$.ajax({
        url: "https://url/api/v2/products",
        beforeSend: function(xhr){
            xhr.setRequestHeader("Authorization", "Basic " + btoa("username" + ":" + "token"));
        },
        type: 'GET',
        dataType: 'json',
        success: function(data){
            console.log("api_success");
        }
    });

如何避免No 'Access-Control-Allow-Origin'?

【问题讨论】:

    标签: ajax api bigcommerce


    【解决方案1】:

    尝试修改数据类型为jsonp

    $.ajax({
            url: "https://url/api/v2/products",
            beforeSend: function(xhr){
                xhr.setRequestHeader("Authorization", "Basic " + btoa("username" + ":" + "token"));
            },
            type: 'GET',
            dataType: 'jsonp',
            success: function(data){
                console.log("api_success");
            }
        });
    

    您也可以在 HTML 中使用:

     <head>...<meta http-equiv="Access-Control-Allow-Origin" content="*">...</head>
    

    但您最好配置您的网络服务器或网络应用程序以发送此标头

    【讨论】:

    • 嗨,阿扎比。我感谢您的帮助。但是,它没有用。它只是返回一个弹出窗口,询问用户名和密码。即使我插入了元
    • var strURL = "/api/v2/products.json"; var auth = demo() //alert(auth) $.ajax({ url: strURL, method: 'GET', dataType: 'json', data: {sku:productCode}, crossDomain: true, beforeSend : function(req ) { req.setRequestHeader('Authorization', auth); },
    • 我在我们的免费演示商店中使用 javascript 中的自定义页面应用此代码,并且总是遇到请求错误未经授权的问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-04
    • 1970-01-01
    • 2012-12-08
    相关资源
    最近更新 更多