[英]Cannot retrieve data from Yelp API using jQuery.ajax()
我試圖使用jQuery.ajax()
方法從Yelp API檢索JSON數據,但它在控制台中不顯示任何內容。
新的Yelp授權只需要將標頭發送為:
"Authorization": "Bearer <apikey>"
以及帶params的GET請求URL。 Postman正在顯示結果,但我不能在我的API中使用它。 我的.ajax
代碼在這里:
var myurl = "https://api.yelp.com/v3/businesses/search?term=by-chloe&location=boston";
$.ajax({
url: myurl,
headers: {
'Authorization': 'Bearer xxxxxxxxxxxxx',
},
method: 'GET',
dataType: 'json',
success: function(data) {
console.log('success: ' + data);
}
});
我需要對此進行任何更改,還是有問題?
我發現我測試你的腳本時的問題只是一個與CORS相關的問題; 通過將CORS-anywhere API附加到URL,我可以使用完全相同的代碼命中您的端點。 嘗試下面的代碼(只需交換您的API密鑰)
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css" integrity="sha384-Smlep5jCw/wG7hdkwQ/Z5nLIefveQRIY9nfy6xoR1uRYBtpZgI6339F5dgvm/e9B" crossorigin="anonymous">
<title>Ilan's Test</title>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/js/bootstrap.min.js" integrity="sha384-o+RDsa0aLu++PJvFqy8fFScvbHFLtbvScb8AjopnFD+iEQ7wo/CG0xlczd+2O/em" crossorigin="anonymous"></script>
<script>
var myurl = "https://cors-anywhere.herokuapp.com/https://api.yelp.com/v3/businesses/search?term=by-chloe&location=boston";
$.ajax({
url: myurl,
headers: {
'Authorization':'Bearer xxxxxxxxxxxxxxxxxxxxxxx',
},
method: 'GET',
dataType: 'json',
success: function(data){
console.log('success: '+data);
}
});
</script>
</body>
</html>
//編輯 - 請參閱下面的代碼,了解如何使用API返回的值。
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css" integrity="sha384-Smlep5jCw/wG7hdkwQ/Z5nLIefveQRIY9nfy6xoR1uRYBtpZgI6339F5dgvm/e9B" crossorigin="anonymous">
<title>Ilan's Test</title>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-lg-12">
<div id="results">
</div>
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/js/bootstrap.min.js" integrity="sha384-o+RDsa0aLu++PJvFqy8fFScvbHFLtbvScb8AjopnFD+iEQ7wo/CG0xlczd+2O/em" crossorigin="anonymous"></script>
<script>
var myurl = "https://cors-anywhere.herokuapp.com/https://api.yelp.com/v3/businesses/search?term=by-chloe&location=boston";
$.ajax({
url: myurl,
headers: {
'Authorization':'Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
},
method: 'GET',
dataType: 'json',
success: function(data){
// Grab the results from the API JSON return
var totalresults = data.total;
// If our results are greater than 0, continue
if (totalresults > 0){
// Display a header on the page with the number of results
$('#results').append('<h5>We discovered ' + totalresults + ' results!</h5>');
// Itirate through the JSON array of 'businesses' which was returned by the API
$.each(data.businesses, function(i, item) {
// Store each business's object in a variable
var id = item.id;
var alias = item.alias;
var phone = item.display_phone;
var image = item.image_url;
var name = item.name;
var rating = item.rating;
var reviewcount = item.review_count;
var address = item.location.address1;
var city = item.location.city;
var state = item.location.state;
var zipcode = item.location.zip_code;
// Append our result into our page
$('#results').append('<div id="' + id + '" style="margin-top:50px;margin-bottom:50px;"><img src="' + image + '" style="width:200px;height:150px;"><br>We found <b>' + name + '</b> (' + alias + ')<br>Business ID: ' + id + '<br> Located at: ' + address + ' ' + city + ', ' + state + ' ' + zipcode + '<br>The phone number for this business is: ' + phone + '<br>This business has a rating of ' + rating + ' with ' + reviewcount + ' reviews.</div>');
});
} else {
// If our results are 0; no businesses were returned by the JSON therefor we display on the page no results were found
$('#results').append('<h5>We discovered no results!</h5>');
}
}
});
</script>
</body>
</html>
請記住交換API密鑰以使上述示例有效。
CORS通常在服務器端啟用; 這是一種防止未經授權的域連接到允許的數據以外的數據的方法; 我想如果你將服務器的IP地址列入白名單,你可能仍會遇到問題; 我已經構建了許多項目,這些項目都需要繞過CORS(使用CORS-Anywhere API,或者發出請求JSONP數據類型的請求)。 Cors-Anywhere通常是一個開源API,您可以在自己的服務器上實現它,或者繼續使用heroku URL; 無論哪種方式,你都應該能夠完成你的項目。
如果以上代碼示例適合您,請告訴我們; 謝謝! - 宜蘭
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.