Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using $.getJSON to retrieve CSV data

I'm trying to retrieve stock data using Yahoo's Finance API:

        $.ajax({
            dataType: "json",
            url: 'http://download.finance.yahoo.com/d/quotes.csv',
            data: 's=RHT+MSFT&f=sb2b3jk&callback=?',
            success: function (d) {
                console.log(JSON.stringify(d));
            },
            error: function (d, a, b) {
                console.log(JSON.stringify(d));
                console.log(JSON.stringify(a));
                console.log(JSON.stringify(b));
            },
            complete: function (d, a, b) {
                console.log(JSON.stringify(d));
                console.log(JSON.stringify(a));
                console.log(JSON.stringify(b));
            }
        });

The call works and I can see the csv text in the response (using Chrome's developer tools):

enter image description here

Now, my problem is I can't access the text contained in the response.

As you can see in the original script, I've tried capturing the responses in the "success", "error", and "complete" callbacks, but the response text is not contained in any of them. Also, only the "error" and "complete" callback is raised.

I'd appreciate any insight on this, thanks in advance!

ps. Reason I'm using the CSV query, as opposed to the YQL query is the CSV query is easier to specify fields I need. I've found the YQL query much more cumbersome to use.

like image 992
Joe C Avatar asked Jun 30 '26 20:06

Joe C


1 Answers

The $.getJSON() method accepts text/json content types and the CSV is not of this type.

This is a shorthand Ajax function, which is equivalent to:

$.ajax({
  dataType: "json",
  url: url,
  data: data,
  success: success
});

The complete executes because it executes regardless of whether the request resulted in a success or error.

like image 75
Konstantin Dinev Avatar answered Jul 02 '26 10:07

Konstantin Dinev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!