Using $ .getJSON to fetch CSV data

I'm trying to get stock data using the Yahoo 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 developer tools):

enter image description here

Now my problem is that I cannot access the text contained in the response.

As you can see in the original script, I tried to capture the responses in the callbacks "success"

, "error"

and "complete"

, but the response text is not contained in any of them. Also, only the callback "error"

and is raised "complete"

.

I would appreciate it, thanks in advance!

ps. The reason I use a CSV query as opposed to a YQL query is a CSV query, which makes it easier to specify the fields I need. I found the YQL query to be more cumbersome to use.

+3


source to share


1 answer


The method $.getJSON()

accepts content types text/json

and is CSV

not of that type.

This is a shorthand Ajax function that is equivalent to:

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

      



Executed complete

because it is executed regardless of whether the request invoked success

or error

.

+4


source







All Articles