3

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.

Joe C
  • 137
  • 1
  • 1
  • 9
  • 4
    CSV is not JSON. `$.getJSON` is, as the name implies, **for JSON.** Why should it be surprising that `$.getJSON` doesn't play nicely with CSV? – Matt Ball Feb 12 '13 at 20:57
  • Side note: use `console.log()` http://stackoverflow.com/questions/4743730/what-is-console-log-and-how-do-i-use-it – Dom Feb 12 '13 at 20:58
  • Have a look at [this answer](http://stackoverflow.com/a/12785546/551322). It could help you. – nrodic Feb 12 '13 at 21:11
  • how do you guys respond so quickly? the community here is so amazing... @MattBall you're absolutely right, trying to get CSV data using JSON data type is rather counter intuitive, I admit. I just haven't found a novel way to retrieve it via javascript (ajax) any other way. Of course, I'm completely open to any other ideas, if any. Thanks! – Joe C Feb 13 '13 at 03:20
  • Use [`$.get`](http://api.jquery.com/jQuery.get) or [`$.ajax`](http://api.jquery.com/jQuery.ajax). – Matt Ball Feb 13 '13 at 03:26

1 Answers1

4

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.

Konstantin Dinev
  • 34,219
  • 14
  • 75
  • 100
  • thank you for the insight, i was actually aware of this. the problem is not initiating the call, it's working with the data contained in the response. i'll post another question below to illustrate (i couldn't post a picture due to ZERO reputation points, but now i have three thanks to you!). – Joe C Feb 13 '13 at 03:25
  • @JoeC What I am saying is that you get a response but the Ajax call does not accept it because it's not of an accepted data type. – Konstantin Dinev Feb 13 '13 at 06:24