2

I can't seem to get a CSV stock file from yahoo finance to "success"-fully load. I've tested different callbacks and suggestions from other questions, but none of them seem to be working - most of them don't output anything.

$(document).ready(function(){
    $.ajax({
        url:"http://finance.yahoo.com/d/quotes.csv?s=XOM&f=sn",
        dataType: 'jsonp',
        success: function(data) {
            alert('good');
        },
        error: function(data) {
            alert(data);
        }
    });
});

​This code alerts [object Object] (the "error" callback), however the CSV file can be seen in the network panel successfully. The data in the network panel reads "XOM, Exxon Mobile Corpo" as expected (so it did load).

I guess the real question is how can I get that data which I know is loaded. I just want to alert it for now... just want to see it on the page. I've spent a countless number of hours fiddling with this and it just doesn't work.

Here's a jsfiddle: http://jsfiddle.net/V94sQ/3/

John Smith
  • 1,750
  • 3
  • 18
  • 31
  • FYI: If you are not using this for personal use, you are breaking the yahoo TOS. http://developer.yahoo.com/forum/General-Discussion-at-YDN/Using-Yahoo-Finance-API-Not-RSS-/1250246255000-0b82f8f0-7f48-3af2-8fe2-e73a138cbfaa#entry8946 – epascarello Dec 07 '12 at 00:34
  • Yeah, I read that already. I'm using it to create my own stock platform (which is customized for my personal usage). It will be running from localhost. – John Smith Dec 07 '12 at 00:36

1 Answers1

2

You can not request CSV files from another domain unless they support CORS. Since you do not control yahoo you are out of luck there. You would need to use a proxy [request it from your own server, backend makes the get request] or a service that can make it into a jsonp request.

epascarello
  • 204,599
  • 20
  • 195
  • 236
  • wait I thought jsonp allows for cross-domain access. I can see the file in my network panel as of now. If it's returning an object, doesn't that mean it worked? – John Smith Dec 07 '12 at 00:41
  • jsonp DOES allow it if the webservice you call supports it. It does not magically transform the response, that would defeat the cross domain policy. – epascarello Dec 07 '12 at 00:46
  • interesting, I tested out the "service" and it works! Thanks, I will switch to this instead. – John Smith Dec 07 '12 at 00:55