2

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?

TruMan1
  • 33,665
  • 59
  • 184
  • 335

3 Answers3

4

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.

vzwick
  • 11,008
  • 5
  • 43
  • 63
1

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.

vzwick
  • 11,008
  • 5
  • 43
  • 63
Moin Zaman
  • 25,281
  • 6
  • 70
  • 74
0

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.

seanb
  • 6,969
  • 2
  • 33
  • 34