如何从RESTFUL webservice获取json响应

时间:2015-03-14 19:37:48

标签: javascript jquery json web-services yahoo-finance

我想提醒此网址http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote?format=json&&callback=的网络服务JSON响应? ,怎么做,有什么想法吗?

我在下面尝试了这个,但它根本不起作用。

 $(document).ready(function () {

    var b = "http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote?format=json&&callback=?";
    //var b= "http://finance.yahoo.com/connection/currency-converter-cache?date=20150307";   // or this url also
    $.getJSON(b, function (e) {
    alert(e);
 });

}) 

FIDDLE


更新:浏览器控制台上显示错误

XMLHttpRequest cannot load http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote?format=json&&callback=?. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://fiddle.jshell.net' is therefore not allowed access.

1 个答案:

答案 0 :(得分:0)

有可能通过YQL使用JSONP访问Yahoo API,但我认为使用其他服务会更好。

雅虎似乎并不支持该API。请参阅此SO question

我在谷歌搜索了一个汇率json api,我发现fixer.io正在运行。请参阅此fiddle及以下。


更新2015年3月14日21:09(UTC)

请查看此jsFiddle它是访问Yahoo API的演示。我必须将env添加到所有数据库才能访问演示的新闻源。您可能可以更改YQL以访问所需的数据。

YQL控制台非常有助于查看可用的数据。您可以访问控制台here

代码来自此page。初始代码无法正常工作,因为它无法找到search.news表。可能是因为env属性,但我需要检查一下。



$(function() {
    $.ajax({
        url: "http://api.fixer.io/latest",
        dataType: "jsonp",
        jsonp: "callback",
        success: function(response) {
            $(".json").text(JSON.stringify(response, null,2));
        }
    });
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<pre class="json"></pre>
&#13;
&#13;
&#13;