簡體   English   中英

使用JSONP從Yahoo Finance加載報價

[英]Loading quotes from Yahoo Finance using JSONP

我希望這會很容易。 但是不,永遠不會發出警報。 請幫忙。

$.getJSON("http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote?format=json&view=basic&callback=?", function(result){
   //response data are now in the result variable
   alert(result);
});

在回調函數中Yahoo JSONP Ajax Request Wrapped嘗試了可接受的答案,但對我也不起作用:(

我從中獲得了回報,但沒有運氣。

var quote;
$(".price").text("please");
$(document).ready(function() {
    $.ajax({
        url: "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%3D%22AAPL%22&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=quote",
        dataType: "jsonp",
        jsonp: "callback",
        jsonpCallback: "quote"
    });

    quote = function(data) {
        $(".price").text("$" + data.query.results.quote.AskRealtime);
    };
});

https://jsfiddle.net/ustj6eob/

我發現ajax調用不起作用的原因是因為Query會自動為您在URL的末尾添加一個時間戳,以確保永遠不會緩存ajax請求。 Yahoo Finance Web服務不支持timestamp參數。

需要添加以下行:

$.ajaxSetup({'cache':true});

這是完整的代碼:

var quote;
$.ajaxSetup({'cache': true});
$(document).ready(function () {
    $.ajax({
        url: "http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote?format=json&view=basic",
       dataType: "jsonp",
       jsonp: "callback",
       jsonpCallback: "quote"
    });

    quote = function (data) {
        var arrayLength = data.list.resources.length;
        var restext = '';
        for (var i = 0; i < arrayLength; i++) {
            restext += "<br>" + data.list.resources[i].resource.fields.name;
        }
        $('#res').html(restext);
    };
});

http://jsfiddle.net/teshg0kn/3/

我也可以在純JS中使用它。

function getQuote(obj) {
    var arrayLength = obj.list.resources.length;
    for (var i = 0; i < arrayLength; i++) { 
        document.getElementById("ip").innerHTML += "<br>" + obj.list.resources[i].resource.fields.name;
    }
}
var url = "http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote?format=json&view=basic&callback=getQuote"
var script = document.createElement('script');
script.setAttribute('src', url);
document.getElementsByTagName('head')[0].appendChild(script);

http://jsfiddle.net/teshg0kn/2/

您在小提琴中做的錯是兩件事:當小提琴使用https時通過http訪問框架,而不在標頭中加載腳本(這使jsonp無法訪問全局query對象)。

養成使用//而不是httphttps來訪問腳本的習慣,然后瀏覽器將為您選擇正確的協議:

var quote;
$(".price").text("please");
$(document).ready(function() {
    $.ajax({
        url: "//query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%3D%22AAPL%22&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=quote",
        dataType: "jsonp",
        jsonp: "callback",
        jsonpCallback: "quote"
    });

    quote = function(data) {
        $(".price").text(data.query.created);
    };
});

https://jsfiddle.net/anm6ebsp/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM