Get CSV file as data in ajax success function

Please consider this javascript:

$.ajax({ url:'http://ichart.finance.yahoo.com/table.csv?s=GS&a=00&b=1&c=2010&d=08&e=3&f=2012&g=d&ignore=.csv', type:'get', dataType:'jsonp', success:function(data){ alert(data); } }) 

The url returns a CSV file, but I am specifying a jsonp data type because it is an ajax request for a cross-domain. Without this parameter, I get the error "origin is not allowed".

Since I am specifying a jsonp data jsonp , the ajax function throws an error because the CSV file is not a JSON format. But in the dev console, I see that the browser receives a coherent CSV file. Therefore, I know that I am successfully receiving a CSV file. I think this should be possible, but I'm not sure how to properly get this csv file for my ajax function ??

Of course, if I could get this URL to return a properly formatted JSON string that would be the best, but I'm not sure I can.

Here is a fiddle where you can try, you have to open the dev console to see this error: http://jsfiddle.net/92uJ4/3/

Any help is greatly appreciated.

Tim

+6
source share
2 answers

Unfortunately, cross-domain restrictions mean that it just won't work. The system is purpose-built, so you cannot pull arbitrary content from multiple domains using AJAX. There is no pre-analysis method for converting non-JSONP data that you get into actual JSONP data (because it will defeat the limit point).

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

To accomplish what you want, use the code in this script: 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']); } }) } 
+7
source

Changing the last jsfiddle provided will lead me to the following solution:
http://jsfiddle.net/9zcsxq5a/

 var str_parse = function(data){ data = data.replace(/<[/]*body[^>]*>/g,''); data = data.replace(/<--[\S\s]*?-->/g,''); data = data.replace(/[\r]+/g,''); data = data.replace(/<noscript[^>]*>[\S\s]*?<\/noscript>/g,''); data = data.replace(/<script[^>]*>[\S\s]*?<\/script>/g,''); data = data.replace(/<script.*\/>/,''); return data } get_url = function(URL){ $.ajax({ url:"http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22"+ encodeURIComponent(URL)+"%22", dataType: "jsonp", type: 'get', success: function(r){ data=r.results[0]; data = str_parse(data); data = data.split(/[\n]+/); //// first line of the csv holds the colnames var HEADER = data[0].split(","); data.shift(); /// create { OBJECT } structure for each row data = (function(){ var o=[]; data.forEach(function(E){ o.push( (function(){ var _o={}; for( var i=0, s=E.split(",");i<s.length;i++ ) _o[HEADER[i]]=s[i]; return _o; }()) ); }); return o; }()); /// THE FINAL OBJECT console.log(data); return data; } }); } $('#a').click(function() { get_url("http://ichart.finance.yahoo.com/table.csv?a=8&b=11&e=10&g=d&c=2005&d=2&f=2016&s=YHOO"); }); 


Csv is passed through asis and will then be modified to become a json object similar to the one (but not limited by date) that you will get when you use

http://query.yahooapis.com/v1/public/yql?q=select%20%2a%20from%20yahoo.finance.historicaldata%20where%20symbol%20in%20%28%27YHOO%27%29%20and% 20startDate% 20 =% 20% 272009-09-11% 27% 20and% 20endDate% 20 =% 20% 272010-03-10% 27 & diagnostics = true & env = store: //datatables.org/alltableswithkeys

0
source

Source: https://habr.com/ru/post/924452/


All Articles