Yelp API Origin http://localhost:8888 is not allowed by Access-Control-Allow-Origin

梦想的初衷 提交于 2019-12-10 15:37:26

问题


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_review_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?


回答1:


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
    }
});


来源:https://stackoverflow.com/questions/3827614/yelp-api-origin-http-localhost8888-is-not-allowed-by-access-control-allow-ori

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!