I have a CSV file hosted on a remote server. Is there a way for jQuery to consume that file so it can be converted to a jQuery array? Would I still be facing cross-site scripting problems?
-
Try this: http://stackoverflow.com/a/12289296/761963 – Jorge Orpinel Pérez Jul 08 '16 at 07:29
3 Answers
You'll be running into restrictions imposed by the Same Origin Policy. In short, AJAX calls to a different domain are prohibited and will always (but for very very rare exceptions) fail, no matter what the content is.
You need to either use JSONP (mostly applicable to data returned by APIs) or proxy the request through your own server/domain.
Consuming the CSV itself is rather trivial:
csv_arr = csvstring.split(/\n/);
$.each(csv_arr, function(i,e){
csv_arr[i] = e.split(',');
});
Edit: Caution, as @echoback mentioned, I have missed the possibility of quoted values.
Luckily, there's an awesome plugin that turns CSV into JSON. Just like that.

- 11,008
- 5
- 43
- 63
-
Remember to consider quoted strings. `"hello, world", 1234` is two elements, not three. – wersimmon Oct 26 '11 at 00:18
-
-
csonv.js doesn't seem to work properly for me (on Firefox). I'm going to try D3: http://learnjsdata.com/read_data.html – Jorge Orpinel Pérez Jul 08 '16 at 07:14
Yes, you'd still have to solve cross domain issues. Your best approach is to use web services on the other end and consume via JSONP and callbacks.

- 11,008
- 5
- 43
- 63

- 25,281
- 6
- 70
- 74
Am not sure on the cross domain issues - but have used jquery ajax to fetch csv files from same domain, then just use javascript split() on line breaks and commas to make arrays.

- 6,969
- 2
- 33
- 34