Receive .csv file as data in ajax success function

前端 未结 2 509
甜味超标
甜味超标 2020-12-06 07:58

Please consider this javascript:

$.ajax({
    url:\'http://ichart.finance.yahoo.com/table.csv?s=GS&a=00&b=1&c=2010&d=08&a         


        
2条回答
  •  眼角桃花
    2020-12-06 08:08

    Unfortunately, cross-domain restrictions mean that this just isn't going to work. The system is built specifically so that you can't pull arbitrary cross-domain content with AJAX. There isn't any sort of pre-parse method to convert the non-JSONP data you're getting into actual JSONP data (because that would defeat the point of the restrictions).

    You're going to have to either make a call to a local server that pulls the data from Yahoo! and sends it to your AJAX request, or find a service of some kind that will pull from an arbitrary URL and return the data as JSONP. As it happens, Yahoo! provides just such a service: YQL (Yahoo query language). See this link for more details.

    To accomplish what you're wanting, use the code in this fiddle: http://jsfiddle.net/c5TeM/1/

    function get_url(remote_url) {
        $.ajax({
            url: "http://query.yahooapis.com/v1/public/yql?"+
    "q=select%20*%20from%20html%20where%20url%3D%22"+
    encodeURIComponent(remote_url)+
    "%22&format=json",
            type: 'get',
            dataType: 'jsonp',
            success: function(data) {
                alert(data.query.results.body.p);
            },
            error: function(jqXHR, textStatus, errorThrow){
                alert(jqXHR['responseText']);
            }
        })
    }
    

提交回复
热议问题