jQuery + JSONP + Yahoo查询语言 [英] jQuery + JSONP + Yahoo Query Language

查看:79
本文介绍了jQuery + JSONP + Yahoo查询语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从外部来源获得实时汇率,所以我发现这个很棒的网络服务:

I want to get live currency rates from an external source, so I found this great webservice:

货币转换器

此服务的运作方式类似于魅力,唯一的缺点是它不提供JSONP结果,只提供XML。因此,在尝试使用jQuery $ .ajax()来使用此Web服务时,我们遇到了跨浏览器问题。

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().

所以我找到了 Yahoo查询语言,它返回结果为JSONP,还有mangae以使用其他Web服务并返回结果。这也有效,这是一个示例网址:

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

此URL返回JSONP结果并且像魅力一样工作,但是我在我的代码中使用此问题时出现问题:

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);
            }
        });

当我尝试运行此代码时没有任何反应,我可以在我的Firebug javascript中看到此错误消息调试器:

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是围绕JSON响应的容器的名称,但为什么说没有定义?

cbfunc is the name of the container which surrounds the JSON response, but why does it say not defined?

编辑:

这是我的新代码,但我仍然得到未定义cbfunc

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");
        }

并且OK消息永远不会被触发......

And the "OK" message is never fired...

推荐答案

如果可用,请在 jsonpCallback 参数> $。ajax 喜欢:

If available, use the jsonpCallback parameter in the call to $.ajax like:

 jsonpCallback: "cbfunc",






其描述来自 jQuery API docs 读取:


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

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

文档后来继续说:


最好让jQuery生成一个唯一的名称,因为它可以更容易地管理请求并提供回调和错误处理。如果要对GET请求进行更好的浏览器缓存,可能需要指定回调。

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.

但是不是建议在使用YQL时使用这种首选行为。正是为什么这种方法不理想可能会使这个答案过于冗长,所以这里有一个链接(来自YQL博客)详细说明jQuery首选方法的问题,利用 jsonpCallback 依此类推: 避免速率限制并在YQL和Pipes中被禁止:缓存是你的朋友

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

这篇关于jQuery + JSONP + Yahoo查询语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆