我们希望通过jQuery / AJAX调用API来在快速搜索结果中实现最低购买量。我们正在尝试调用API,但没有得到响应。我们收到以下错误消息:
NetworkError:401未授权- https://mystore.mybigcommerce.com/api/v2/products/product_id
以下是我们在quicksearch.js文件中添加的代码。
var key = 'API key';
var auth = 'Basic ' + btoa('username:'+key);
var url = 'https://mystore.mybigcommerce.com/api/v2/products/product_id';
$.ajax({
url : url,
method : 'GET',
dataType: "json",
contentType: "application/json; charset=utf-8",
async: false,
crossDomain: true,
beforeSend : function(req) {
req.setRequestHeader('Authorization', auth);
},
success: function(result) {
alert('done');
console.log(result);
},
error: function (request, textStatus, errorThrown) {
console.log(request.responseText);
console.log(textStatus);
console.log(errorThrown);
}
});
谁能指导解决错误?
您可以在实时服务器上创建单独的PHP应用程序以调用Big-commerce API。
您可以从BigCommerce Store-管理面板->高级设置->旧版API设置中创建旧版API帐户,并可以获取API网址,用户名,令牌。
您可以实施以下代码来获取最小购买数量。
文件名为getproductinfo.php。
<?php
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
$product_id = $_GET['prod_id'];
$username='username';
$password='API token';
$URL='https://mystoreurl.mybigcommerce.com/api/v2/products/'.$product_id;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:UTF-8','Accept: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //get status code
$result=curl_exec ($ch);
curl_close ($ch);
$data = json_decode($result,true);
$minimimOrder = $data['order_quantity_minimum'];
echo $minimimOrder;die();
?>
响应(最低购买数量) - >您可以在quicksearch.js文件中使用下面的代码作为获取结果。
var url = 'http://liveserveripaddress/foldername/getproductinfo.php';
$.ajax({
url : url,
type : 'GET',
data : {prod_id:productid},
dataType: "json",
crossDomain: true,
success: function(result) {
console.log(result);
},
error: function (request, textStatus, errorThrown) {
console.log(request.responseText);
console.log(textStatus);
console.log(errorThrown);
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句