jQuery + JSONP +雅虎查询语言

发布于 2024-09-07 03:04:40 字数 1757 浏览 0 评论 0 原文

我想从外部源获取实时货币汇率,因此我找到了这个很棒的网络服务:

货币转换器

这项服务运行得非常出色,唯一的缺点是它不提供 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...

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

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

发布评论

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

评论(4

女皇必胜 2024-09-14 03:04:40

如果可用,请在调用 $.ajax 时使用 jsonpCallback 参数,例如:

 jsonpCallback: "cbfunc",

其描述,来自 jQuery API 文档 内容如下:

指定 jsonp 请求的回调函数名称。将使用该值代替 jQuery 自动生成的随机名称。

文档后来继续说:

最好让 jQuery 生成一个唯一的名称,因为这样可以更轻松地管理请求并提供回调和错误处理。当您想要启用更好的 GET 请求浏览器缓存时,您可能需要指定回调。

但是,建议在使用 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:

 jsonpCallback: "cbfunc",

Its description, from the jQuery API docs reads:

Specify the callback function name for a jsonp request. This value will be used instead of the random name automatically generated by jQuery.

The docs later go on to say:

It is preferable to let jQuery generate a unique name as it'll make it easier to manage the requests and provide callbacks and error handling. You may want to specify the callback when you want to enable better browser caching of GET requests.

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

離人涙 2024-09-14 03:04:40

您应该通过将 urlToWebservice 更改为以 callback=? 结尾,让 jQuery 处理回调

You should let jQuery handle the callback by changing urlToWebservice to end in callback=?

開玄 2024-09-14 03:04:40

它不起作用的原因是,通过在查询字符串中指定 callback=cbfunc 会生成以下类型的 URL:

http://query.yahooapis.com/...&callback=cbfunc&callback=jsonp1277417828303

去掉所有不感兴趣的部分,但 URL 包含两个 callback 参数。其中一个由 jQuery 管理,另一个则不是。 YQL 仅查看第一个回调参数并返回围绕该参数的响应。

cbfunc({"query":{...}});

但是,您的脚本中没有名为 cbfunc 的函数,因此这就是您收到未定义错误的原因。 jQuery 在上面的示例中创建了一个名为 jsonp1277417828303 的隐式函数,而来自 YQL 的响应应该是:

jsonp1277417828303({"query":{...}});

让 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:

http://query.yahooapis.com/...&callback=cbfunc&callback=jsonp1277417828303

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 first callback parameter and returns a response wrapped around that.

cbfunc({"query":{...}});

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 named jsonp1277417828303 in the above example, and the response from YQL should instead have been:

jsonp1277417828303({"query":{...}});

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=cbfuncfrom your URL, or replace it with &callback=? to let jQuery handle things.

See a working example.

近箐 2024-09-14 03:04:40

你绝对应该尝试一下 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 :)

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