简体   繁体   English

从Javascript调用Ajax Yelp API

[英]Ajax Yelp API Call from Javascript

I'm trying to make a call to the Yelp API from JavaScript, but getting an error. 我正在尝试从JavaScript调用Yelp API,但出现错误。 Below is my code. 下面是我的代码。 I believe I will have to use Oauth, but I don't know where should I put it in the header. 我相信我将不得不使用Oauth,但是我不知道应该将它放在标题中的哪个位置。

function doAjax(){
    var xhr = new XMLHttpRequest();
    var url = "http://api.yelp.com/v2/searchterm=cream+puffs&location=chicago";
    xhr.onreadystatechange = function(){
        if(xhr.readyState == 4 && xhr.status == 200){
        var some = JSON.parse(xhr.responseText);
        }
    }
    xhr.open('GET', url, true);
    xhr.send();
}

The problem is that you are trying to access a resource that is on a different domain from your application. 问题是您正在尝试访问与应用程序位于不同域中的资源。 In this case your application resides on http://fiddle.jshell.net and the resource is at http://api.yelp.com . 在这种情况下,您的应用程序位于http://fiddle.jshell.net上 ,资源位于http://api.yelp.com上

CORS is one way to get around this, see here: http://en.wikipedia.org/wiki/Cross-origin_resource_sharing CORS是解决此问题的一种方法,请参见此处: http : //en.wikipedia.org/wiki/Cross-origin_resource_sharing

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM