1

I was hoping it would be this easy. But no, alert is never called. Please help.

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

I tried the accepted answer on Yahoo JSONP Ajax Request Wrapped in callback function but that doesn't work for me either :(

I made a jsfiddle from that but no luck.

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/

Jan
  • 5,688
  • 3
  • 27
  • 44
user1411626
  • 81
  • 1
  • 1
  • 4
  • possible duplicate of [Yahoo JSONP Ajax Request Wrapped in callback function](http://stackoverflow.com/questions/6567119/yahoo-jsonp-ajax-request-wrapped-in-callback-function) –  Jul 24 '15 at 14:55
  • Look at the error message you get in the browser's error console when you visit that JS Fiddle URL. – Quentin Jul 24 '15 at 15:34
  • The error message was just because of using https. Changing the link to http://jsfiddle.net/ustj6eob/ and the error message disappears but the callback is still never called :( – user1411626 Jul 24 '15 at 15:51

2 Answers2

0

I found out that the reason the ajax call did not work is because Query will automatically add a timestamp to the end of the URL for you, making sure that ajax requests are never cached. The timestamp parameter is is not supported by the Yahoo Finance web service.

The following line needs to be added:

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

Here is the entire code:

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/

I got it working in pure JS as well.

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/

user1411626
  • 81
  • 1
  • 1
  • 4
  • While this might be a good general tip, I don't see how this answers the question you originally posted. – Jan Jul 26 '15 at 16:29
0

What you were doing wrong in your fiddle was two things: accessing the framework through http when fiddle uses https and not loading the script in the header (which gave the jsonp trouble accessing the global query object).

Make a habit of accessing scripts with // instead of http or https, the browser will then choose the correct protocol for you:

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/

Jan
  • 5,688
  • 3
  • 27
  • 44