使用 YQL 与 JQuery 进行跨域请求

发布于 2024-09-08 09:06:11 字数 1101 浏览 4 评论 0原文

所以我需要发出一个跨域请求,其中响应不是 JSON 格式,所以我不能使用 .getJSON。 .get 显然不起作用,因为它是跨域请求。

当我谷歌搜索,它似乎应该适合我想做的事情(即使用 jquery 插件进行非 json 格式的跨域调用)。我的代码如下所示。我知道该网址工作正常,因为如果我将其粘贴到浏览器中,我可以看到响应,根据 last.fm 文档

服务器响应的正文 由一系列 \n (ASCII 10) 组成 终止线。一个典型的成功者 服务器响应将类似于 这个:

OK
17E61E13454CDD8B68E8D7DEEEDF6170
http://post.audioscrobbler.com:80/np_1.2
http://post2.audioscrobbler.com:80/protocol_1.2

所以我知道我的网址没问题。现在我想知道如何获取这些信息,以及为什么我的示例版本不起作用。

function performHandshake(sk, token, ts){

    var token = md5(apiSecret + ts);
    var urlToUse = "http://post.audioscrobbler.com/?hs=true&p=1.2.1&c=tst&v=1.0&u=chamals&t=" + ts + "&a=" + token + "&api_key=" + apiKey + "&sk=" + sk + "&format=xml&callback=cbfunc";
            $('#container').load(urlToUse);
    $.ajax({
        url: urlToUse,
        type: 'GET',
        success: function(res){
            var headline = $(res.responseText).find('a.tst').text();
            window.console.log(headline);   
        }   
    });

}

So I need to make a a cross domain request where the response is not JSON formatted, so I cannot use .getJSON. .get obviously doesn't work because it is a cross domain request.

I came across this (Read this) when I was googling and it seems it should work for what I want to do (which is do a cross domain call that isn't json formatted using a jquery plug in). My code looks like the following. I know the url works fine because if I paste it into my browser, I can see the response, which according to last.fm documentation

The body of the server response
consists of a series of \n (ASCII 10)
terminated lines. A typical successful
server response will be something like
this:

OK
17E61E13454CDD8B68E8D7DEEEDF6170
http://post.audioscrobbler.com:80/np_1.2
http://post2.audioscrobbler.com:80/protocol_1.2

So I know my URL is fine. Now I am wondering how I get at this information, and why my version of their example does not work.

function performHandshake(sk, token, ts){

    var token = md5(apiSecret + ts);
    var urlToUse = "http://post.audioscrobbler.com/?hs=true&p=1.2.1&c=tst&v=1.0&u=chamals&t=" + ts + "&a=" + token + "&api_key=" + apiKey + "&sk=" + sk + "&format=xml&callback=cbfunc";
            $('#container').load(urlToUse);
    $.ajax({
        url: urlToUse,
        type: 'GET',
        success: function(res){
            var headline = $(res.responseText).find('a.tst').text();
            window.console.log(headline);   
        }   
    });

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冷情 2024-09-15 09:06:11

那么您链接的页面讨论了使用 YQL 和 jQuery。这是一个非常有趣的解决方案。但是,您的示例似乎跳过了 YQL 部分(这很重要)。

var urlToUse = "http://post.audioscrobbler.com/?hs=true&p=1.2.1&c=tst&v=1.0&u=chamals&t=" + ts + "&a=" + token + "&api_key=" + apiKey + "&sk=" + sk + "&format=xml&callback=cbfunc";

var yqlUrl2Use = "http://query.yahooapis.com/v1/public/yql?"+
            "q=select%20*%20from%20html%20where%20url%3D%22"+
            encodeURIComponent(urlToUse)+
            "%22&format=xml'&callback=?"
    // this function gets the data from the successful 
    // JSON-P call

然后,您必须将新 URL 作为 JSONP 请求进行调用...

$.getJSON(yqlUrl2Use, function(json){
    // figure out the format of the answer here...   
});

Well the page you linked you talks about using YQL and jQuery. It's a very interesting solution. However, your example seems to skip over the YQL part (which is crucial).

var urlToUse = "http://post.audioscrobbler.com/?hs=true&p=1.2.1&c=tst&v=1.0&u=chamals&t=" + ts + "&a=" + token + "&api_key=" + apiKey + "&sk=" + sk + "&format=xml&callback=cbfunc";

var yqlUrl2Use = "http://query.yahooapis.com/v1/public/yql?"+
            "q=select%20*%20from%20html%20where%20url%3D%22"+
            encodeURIComponent(urlToUse)+
            "%22&format=xml'&callback=?"
    // this function gets the data from the successful 
    // JSON-P call

Then you'll have to call the call the new URL as a JSONP req...

$.getJSON(yqlUrl2Use, function(json){
    // figure out the format of the answer here...   
});
不即不离 2024-09-15 09:06:11

是的,跨浏览器脚本。您不能使用 AJAX 进行类似的操作,因为它违反了相同的域策略。

您必须在运行 JavaScript 的同一服务器上设置代理。

编辑 看起来您需要 $('#container').load(url) 位才能正常工作。

返回并仔细阅读链接的文章。

Yeah, cross browser scripting. You can't AJAX anything like that since it violates the same domain policy.

You are going to have to setup a proxy on the same server the JavaScript is running from.

Edit Lookslike you need the $('#container').load(url) bit for that to work.

Go back an reread the linked article carefully.

小兔几 2024-09-15 09:06:11

您需要使用 $.getJSON 而不是 $.ajax() 返回跨站信息。

You need to use $.getJSON rather than $.ajax() to return cross site information.

违心° 2024-09-15 09:06:11

var res 实际上有我需要的信息。我猜他们的标题=部分是专门为他们的实施而设计的。

感谢那些提供帮助的人!

The var res actually has my information that I needed. I guess their headline = part was specifically for their implementation.

Thanks to those who helped!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文