Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
496 views
in Technique[技术] by (71.8m points)

javascript - Cross-Domain get CSV file

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/

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...