我收到了错误
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'
?
答案 0 :(得分:0)
尝试将数据类型修改为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>
但您最好配置您的网络服务器或网络应用程序以发送此标题