Yahoo JSONP Ajax Request Wrapped in callback function

前端 未结 5 2003
孤独总比滥情好
孤独总比滥情好 2020-12-06 21:38

I understand that I can make a crossdomain ajax call with jquery, .ajax, and jsonp. I am calling the yahoo stock quote api. Everything is working and the result is returning

5条回答
  •  生来不讨喜
    2020-12-06 22:15

    Here is a js fiddle for this: https://jsfiddle.net/vham369w/1/

    using $.getJson instead of $.ajax

    JS

    $(document).ready(function() {
        var symbol = 'AAPL'
        var url =  "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%3D%22"+symbol+"%22&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";
        $.getJSON(url + "&callback=?", null, function(data) {
            console.log(data);
            $("#realtime").text("$" + data.query.results.quote.AskRealtime);
            $("#ask").text("$" + data.query.results.quote.Ask);
        });
    });
    

    HTML

    Ask:
    
    loading ask
    Realtime (null if market is closed):
    loading realtime

提交回复
热议问题