Using the following code, I get the error in the title of this question using Chrome's JavaScript developer console:
jQuery.getJSON("http://api.yelp.com/business_revie开发者_开发问答w_search?term=starbucks&location=Urbana%20IL&limit=3&ywsid=XXX",
function(data){
jQuery.each(data, function(i,businesses){
jQuery("#yelpPreview").append(businesses.url);
if ( i == (amount - 1) ) return false;
});
});
In full, the error is: XMLHttpRequest cannot load http://api.yelp.com/business_review_search?term=starbucks&location=Urbana%20IL&limit=3&ywsid=XXX. Origin http://localhost:8888 is not allowed by Access-Control-Allow-Origin.
I'm using MAMP as my localhost.
Is this an issue with Yelp blocking API access to a localhost, or is there an error in my code?
Looks like you are using jQuery. The 'jsonp' option for datatype provided by jQuery's ajax call is a more elegant solution to this, as a short example:
$.ajax({
url : 'http://api.yelp.com/business_review_search',
dataType : 'jsonp',
data : {term : 'restaurant', lat : xxx, long : xxx}, // callback is not necessary
success : function(data) {
// data is a normal response shown on yelp's API page
}
});
精彩评论