Access-Control-Allow-Origin不允许Yelp API来源http:// localhost:8888 [英] Yelp API Origin http://localhost:8888 is not allowed by Access-Control-Allow-Origin
问题描述
使用以下代码,使用Chrome的JavaScript开发人员控制台,在此问题的标题中出现了错误:
jQuery。 getJSON( http://api.yelp.com/business_review_search?term=starbucks&location=Urbana%20IL&limit=3&ywsid=XXX,
函数(数据){
jQuery.each (data,function(i,businesses){
jQuery(#yelpPreview)。append(businesses.url);
if(i ==(amount-1))return false;
});
});
完整,错误是:XMLHttpRequest无法加载 http://api.yelp.com/business_review_search?term=starbucks&location=Urbana% 20IL& limit = 3& ywsid = XXX 。 Access-Control-Allow-Origin不允许使用来源 http:// localhost:8888 。
我使用MAMP作为我的本地主机。
这是Yelp阻止API访问本地主机的问题,还是有错误
好像在使用jQuery。举一个简短的例子,jQuery的ajax调用为数据类型提供了 jsonp选项:
$ .ajax({
url:'http://api.yelp.com/business_review_search',
dataType:'jsonp',
data:{term:'restaurant',lat:xxx ,long:xxx},//不需要回调
成功:function(data){
//数据是yelp的API页面上显示的正常响应
}
}) ;
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?
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
}
});
这篇关于Access-Control-Allow-Origin不允许Yelp API来源http:// localhost:8888的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!