1

I have a little problem of not being abble to retrieve csv response.

There is a csv available here link to csv

I am trying to retrieve this data, however there is a problem with special characters in the reponse.

I successfuly recieve the data but then jQuery tries to decode it and through a syntax error: My request is:

$.ajax({
    url: 'http://toolserver.org/~daniel/WikiSense/Contributors.php?wikilang=en&wikifam=.wikipedia.org&page=Cloud+computing&since=&until=&grouped=on&order=-edit_count&max=1000&order=-edit_count&format=csv',
    dataType: "script",
    success: function(data){
        alert('success');
    },
    error: function(test1, test2, test3) {
        alert('error');
    }
});

The error is (pointing at % character)

SyntaxError: syntax error
1004+%28922%2F82%29,SamJohnston,2008-07-26+09%3A40,2013-03-07+18%

I also tried to set the dataType to text in order to not decode the data. In this case I get an error for the ajax request.

$.ajax({
    url: 'http://toolserver.org/~daniel/WikiSense/Contributors.php?wikilang=en&wikifam=.wikipedia.org&page=Cloud+computing&since=&until=&grouped=on&order=-edit_count&max=1000&order=-edit_count&format=csv',
    dataType: "text",
    success: function(data){
        alert('success');
    },
    error: function(test1, test2, test3) {
        alert('error');
    }
});

I tried to experiment with .ajax() parameters contentType, dataType, scriptCharset etc. It does not help.

eveferon
  • 33
  • 1
  • 5

2 Answers2

1

dataType: "text" would be correct. It sounds like you're running into the Same Origin Policy, which prevents cross-domain calls. When you said dataType: "script", jQuery automatically converted the request into adding a script element rather than doing an actual ajax call. Adding script elements that reference scripts cross-domain isn't a violation of the SOP (which is why we can use CDNs like Google's and Microsoft's for common scripts). But you can't do that when grabbing the CSV, because the CSV isn't (of course) valid JavaScript code.

If the server you're retrieving from is under your control, and if you're using a modern browser (which in this case means any vaguely recent version of Chrome, Firefox, or Opera, or using IE9 or higher), you can implement Cross-Origin Resource Sharing, which allows cross-origin ajax calls if the server allows the source origin of the requesting document. CORS basically means responding to an HTTP OPTIONS call that asks whether it's okay to send the ajax call with appropriate headers.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Every time I run into this cross-origin problem. In this case it is eventually a simple plain text I am trying to retrieve. What might be the reason for this to violate cross-origin policy? What is different to me puting this url into browser and getting this plain text displayed? The server is unfortunately not under my control. – eveferon Mar 25 '13 at 08:15
  • @eveferon: There's a huge difference between a web page that the user loads requesting a resource from a completely different location and the user themselves doing it (by pasting the URL into the address bar). There are several reasons for the SOP, you may find [this question and its answer](http://stackoverflow.com/questions/1830050/why-same-origin-policy-for-xmlhttprequest) helpful. – T.J. Crowder Mar 25 '13 at 08:35
  • Thank you for the answer. I am sure there are a lot of people wondering about the same question. So the simplified message for people having the same problem is: If your browser displays something from a foreign domain and has access to the corresponding objects it does not necessarily mean you can access it programmatically. – eveferon Mar 25 '13 at 14:30
0

It seems to me that this is a cross-domain request which is not acceptable. Read here and here for more info

Community
  • 1
  • 1
i100
  • 4,529
  • 1
  • 22
  • 20