user1156544 user1156544 - 1 year ago 144
Ajax Question

Get CSV via Ajax avoiding CORS

I have a page that uses a ZingChart and loads the data via Ajax from multiple URLs in other domains. The data are in CSV files such as:

1, 2, 3, 4, 5



What I need is to access these files and construct a JSON for ZingChart in this way:

{"data":[THE_CSV_GOES_HERE], "name":"WHATEVER"}

My first problem is that I ran into the usual CORS problem (whose usefulness I still don't understand very well), and since I don't have control on the servers hosting the CSVs, I used this workaround as explained here:

url:"http://otherdomain/test.csv", // This is dynamically placed
dataType: 'jsonp',
return res;
error:function(r, error){
alert("Error " + error);

This raises a
, I suppose because the CSV is not in json format.

If I use "text" instead of "jsonp" I get the CROS error (
No 'Access-Control-Allow-Origin' header is present on the requested resource.
) If I use "jsonp text" as in the documentation, I still get the
. If I use
crossDomain: true, dataType: 'text'
to force a crossDomain but specifying it is text, I still get the CORS error.

It seems that I am having a similar problem as this person.

How can I overcome this? I cannot believe that in the modern Web it is not possible to get a string of text from a distributed server without refreshing the page. It looks like a strong limitation. In fact, I would think it is the client who should be able to allow cross domains, not the external servers (which are normally outside the client's control).

Answer Source

You could try getting the csv file with a server side request as outlined here and then just make your ajax call to your server side web method to get the csv string.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download