[英]Cross-domain requests with JQuery using YQL
So I need to make aa cross domain request where the response is not JSON formatted, so I cannot use .getJSON. 因此,我需要发出一个跨域请求,其中的响应不是JSON格式的,因此我不能使用.getJSON。 .get obviously doesn't work because it is a cross domain request.
.get显然不起作用,因为它是跨域请求。
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). 我在谷歌搜索时遇到了这个问题( 阅读本文 ),看来它应该可以实现我想做的事情(这是一个跨域调用,它不是使用jquery插件进行json格式化的)。 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
我知道url可以正常工作,因为如果将其粘贴到浏览器中,我可以看到响应,根据last.fm文档
The body of the server response consists of a series of \\n (ASCII 10) terminated lines.
服务器响应的主体由一系列\\ n(ASCII 10)终止的行组成。 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);
}
});
}
Yeah, cross browser scripting. 是的,跨浏览器脚本。 You can't AJAX anything like that since it violates the same domain policy.
您无法通过AJAX进行此类操作,因为它违反了相同的域策略。
You are going to have to setup a proxy on the same server the JavaScript is running from. 您将必须在运行JavaScript的服务器上设置代理。
Edit Lookslike you need the
$('#container').load(url)
bit for that to work. 编辑看起来您需要
$('#container').load(url)
位才能正常工作。
Go back an reread the linked article carefully. 返回并仔细阅读链接文章。
Well the page you linked you talks about using YQL and jQuery. 好吧,您链接的页面谈到了使用YQL和jQuery。 It's a very interesting solution.
这是一个非常有趣的解决方案。 However, your example seems to skip over the YQL part (which is crucial).
但是,您的示例似乎跳过了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
Then you'll have to call the call the new URL as a JSONP req... 然后,您必须将调用新的URL作为JSONP请求调用。
$.getJSON(yqlUrl2Use, function(json){
// figure out the format of the answer here...
});
您需要使用$.getJSON
而不是$.ajax()
来返回跨站点信息。
The var res actually has my information that I needed. var res实际上拥有我需要的信息。 I guess their headline = part was specifically for their implementation.
我猜他们的标题=部分专门针对其实现。
Thanks to those who helped! 感谢那些帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.