使用 YQL 与 JQuery 进行跨域请求
所以我需要发出一个跨域请求,其中响应不是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
那么您链接的页面讨论了使用 YQL 和 jQuery。这是一个非常有趣的解决方案。但是,您的示例似乎跳过了 YQL 部分(这很重要)。
然后,您必须将新 URL 作为 JSONP 请求进行调用...
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).
Then you'll have to call the call the new URL as a JSONP req...
是的,跨浏览器脚本。您不能使用 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.
您需要使用
$.getJSON
而不是$.ajax()
返回跨站信息。You need to use
$.getJSON
rather than$.ajax()
to return cross site information.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!