我想从外部源获取实时货币汇率,因此我找到了这个很棒的网络服务:
货币转换器
这项服务运行得非常出色,唯一的缺点是它不提供 JSONP 结果,只提供 XML。因此,当我们尝试使用 jQuery $.ajax() 使用此 Web 服务时,会遇到跨浏览器问题。
所以我找到了 Yahoo Query Language ,它以 JSONP 形式返回结果,并且还 mangae 来使用其他网络服务并向我返回结果。这也有效,这里是一个示例 URL:
http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20xml%20where%20url%3D'http%3A%2F%2Fwww.webservicex.net%2FCurrencyConvertor.asmx%2FConversionRate%3FFromCurrency%3DNOK%26ToCurrency%3DEUR'&format=json&diagnostics=true&callback=cbfunc
此 URL 返回 JSONP 结果并且工作得像一个魅力,但是当我在代码中使用它时出现问题:
$.ajax({
type: "GET",
url: urlToWebservice,
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
success: function(data) {
$("#status").html("OK: " + data.text);
},
error: function(xhr, textStatus, errorThrown) {
$("#status").html("Unavailable: " + textStatus);
}
});
当我尝试运行此代码时没有任何反应,我可以看到这个我的 Firebug javascript 调试器中出现错误消息:
cbfunc is not defined
cbfunc 是包含 JSON 响应的容器的名称,但为什么它说未定义?
编辑:
这是我的新代码,但我仍然得到cbfunc未定义
$.ajax({
url: "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20xml%20where%20url%3D'http%3A%2F%2Fwww.webservicex.net%2FCurrencyConvertor.asmx%2FConversionRate%3FFromCurrency%3DNOK%26ToCurrency%3DEUR'&format=json&callback=cbfunc",
dataType: 'jsonp',
jsonp: 'callback',
jsonpCallback: 'cbfunc'
});
function cbfunc(data) {
alert("OK");
}
并且“OK”消息从未被触发......
I want to get live currency rates from an external source, so I found this great webservice:
Currency Convertor
This service is working like a charm, the only downside is that it does not provide JSONP results, only XML. Therefore we have a cross browser problem while trying to consume this webservice using jQuery $.ajax().
So I found Yahoo Query Language which returns results as JSONP and also mangae to consume other webservices and return me the results. This is also working, here is an example URL:
http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20xml%20where%20url%3D'http%3A%2F%2Fwww.webservicex.net%2FCurrencyConvertor.asmx%2FConversionRate%3FFromCurrency%3DNOK%26ToCurrency%3DEUR'&format=json&diagnostics=true&callback=cbfunc
This URL return JSONP result and is working like a charm, but the problem appears when I use this in my code:
$.ajax({
type: "GET",
url: urlToWebservice,
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
success: function(data) {
$("#status").html("OK: " + data.text);
},
error: function(xhr, textStatus, errorThrown) {
$("#status").html("Unavailable: " + textStatus);
}
});
When I try to run this code nothing happens, and I can see this error message in my Firebug javascript debugger:
cbfunc is not defined
cbfunc is the name of the container which surrounds the JSON response, but why does it say not defined?
EDIT:
This is my new code, but I still get the cbfunc is not defined
$.ajax({
url: "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20xml%20where%20url%3D'http%3A%2F%2Fwww.webservicex.net%2FCurrencyConvertor.asmx%2FConversionRate%3FFromCurrency%3DNOK%26ToCurrency%3DEUR'&format=json&callback=cbfunc",
dataType: 'jsonp',
jsonp: 'callback',
jsonpCallback: 'cbfunc'
});
function cbfunc(data) {
alert("OK");
}
And the "OK" message is never fired...
发布评论
评论(4)
如果可用,请在调用
$.ajax
时使用jsonpCallback
参数,例如:其描述,来自 jQuery API 文档 内容如下:
文档后来继续说:
但是,不建议在使用 YQL 时使用这种“首选”行为。确切地说,为什么这种方法不理想可能会使这个答案过于冗长,因此这里有一个链接(来自 YQL 博客),详细介绍了 jQuery 首选方法的问题,使用
jsonpCallback
等等:< a href="http://www.yqlblog.net/blog/2010/03/12/avoiding-rate-limits-and-getting-banned-in-yql-and-pipes-caching-is-your-friend/ " rel="noreferrer">避免 YQL 和管道中的速率限制和被禁止:缓存是您的朋友If available, use the
jsonpCallback
parameter in the call to$.ajax
like:Its description, from the jQuery API docs reads:
The docs later go on to say:
However it is not advised to make use of this "preferable" behaviour when making use of YQL. Precisely why that approach is not ideal might make this answer far too verbose, so here is a link (from the YQL blog) detailing the problems with jQuery's preferred approach, making use of
jsonpCallback
and so on: Avoiding rate limits and getting banned in YQL and Pipes: Caching is your friend您应该通过将
urlToWebservice
更改为以callback=?
结尾,让 jQuery 处理回调You should let jQuery handle the callback by changing
urlToWebservice
to end incallback=?
它不起作用的原因是,通过在查询字符串中指定
callback=cbfunc
会生成以下类型的 URL:去掉所有不感兴趣的部分,但 URL 包含两个
callback
参数。其中一个由 jQuery 管理,另一个则不是。 YQL 仅查看第一个回调参数并返回围绕该参数的响应。但是,您的脚本中没有名为
cbfunc
的函数,因此这就是您收到未定义错误的原因。 jQuery 在上面的示例中创建了一个名为jsonp1277417828303
的隐式函数,而来自 YQL 的响应应该是:让 jQuery 对其进行操作,并将响应返回给您的
success
它从来没有做过的回调。因此,正如 @SLaks 建议的那样,从 URL 中删除
&callback=cbfunc
,或将其替换为&callback=?
以让 jQuery 处理事务。请参阅工作示例。
The reason it's not working is because by specifying
callback=cbfunc
in the querystring generates a URL of the type:Stripped out all uninteresting parts, but the URL contains two
callback
parameters. One of them is managed by jQuery, and the other one not. YQL only looks at the firstcallback
parameter and returns a response wrapped around that.However, there is no function named
cbfunc
in your script, so that's why you are getting the undefined error. jQuery created an implicit function namedjsonp1277417828303
in the above example, and the response from YQL should instead have been:for jQuery to act upon it, and return the response to your
success
callback which it never got to do.So, as @SLaks suggested, remove the
&callback=cbfunc
from your URL, or replace it with&callback=?
to let jQuery handle things.See a working example.
你绝对应该尝试一下 jQuery-JSONP: http://code.google.com/p /jquery-jsonp/
简化一切:)
You definitely should give jQuery-JSONP a try: http://code.google.com/p/jquery-jsonp/
Simplifies everything :)