Diskdrive
Diskdrive

Reputation: 18825

How do I figure out why a Jquery AJAX request has failed?

I'm trying to call the Yahoo Finance api which returns some data in the form of a CSV file.

I'm trying to do this in Javascript and as per below but it's failing.

$.ajax({
type: "GET",
url: "http://finance.yahoo.com/d/quotes.csv?s=RHT+MSFT&f=sb2b3jk",
dataType: "text",
success: function(data) { 
    // when successful
}
  }).error(function(jqXHR, textStatus, errorThrown) {
               console.log("error " + textStatus);
        console.log("incoming Text " + jqXHR.responseText);
        console.log("error THrown " + errorThrown);
        });

How do I figure out what the error message is? I've tried putting a callback on the error, but all the parameters are empty except for textStatus which is returning "error".

Upvotes: 1

Views: 183

Answers (1)

adeneo
adeneo

Reputation: 318182

It just so happens that a .csv file can't be downloaded with ajax (unless CORS is enabled etc), but you seem to be looking for stock quotes from Yahoo Finance, and Yahoo offers this information both in YQL and in Pipes, so here's a really quick example for YQL:

var tick = 'MSFT';

var url = 'http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22'+tick+'%22)%0A%09%09&format=json&diagnostics=true&env=http%3A%2F%2Fdatatables.org%2Falltables.env&callback=?';

$.getJSON(url, function(data) {
    console.log(data.query.results);
});

FIDDLE

And here it is in the YQL CONSOLE where you can try out different queries against Yahoo's data, and this is all available as XML or JSON with ajax, so you don't need the .csv file, you should get the data directly from YQL.

Upvotes: 2

Related Questions