Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XMLHttpRequest xml response woes with jQuery 1.4.1, how to force the request response to be processed as plain text?

Tags:

jquery

I'm just playing around with jQuery and trying something that ought be simple, but it just ain't working.

    $(document).ready(function(){
 $.ajax({
  url : 'http://soiduplaan.tallinn.ee/',
  data : {a : 'p.routes', transport_id : 'tram', t : 'xml', l : 'ee'},
  error : function(xhr, stat){
   alert('error'); 
  },
  success : function(data){
   alert('success');
   alert(data);
  }
 });
});

The snippet is in a test.js file and included in a test.html file which is opened in Firefox (3.6) like file:///C:/test.html and altough 'success' is shown the data is empty and through Firebug the response for XML displays:

X`ML Parsing Error: no element found Location: moz-nullprincipal:{5ac44e50-2cb6-45d1-9cfe-0b999850ecdb} Line Number 1, Column 1:`

Alternatively I tried that adding

dataType : "text"

has no effect, the result is still processed as xml (probably because the response has content-type: text/xml; charset=UTF-8).

I'm able to see the response results through Firebug if I set

dataType : "script"

but then as it isn't actually a valid js script it simply fails, Firebug displays:

    invalid regular expression flag t
<?xml version="1.0" encoding="UTF-8"?>..._days><types><type routes="85" city="t

It gets "better", if do the above request in browser and through "View Source" copy the xml to be validated here http://www.w3schools.com/Dom/dom_validate.asp it displays "no errors found" so why won't it work through xmlhttprequest?

What am I doing wrong?

Would it be possible to somehow force the xmlhttprequest's response to be processed as text/plain?

br, iges

P.S. I've tired the suggested dataType : "html" option (forgot to mention initially), but the "html" also does not work and in Firebug I can see the same error about parsing/moz-nullprincipal.

Also the service providing the xml data is controlled by a third party to which I do not have access, either there is a way to do this or I'll have to kiss my idea goodbye :(

like image 952
iges Avatar asked Jan 29 '10 13:01

iges


2 Answers

Are you executing this code from the same domain ? (http://soiduplaan.tallinn.ee)

Because you are not allowed to use the ajax calls to fetch data from other domains ... (unless using the JSONP or script datatypes ..)

When data is retrieved from remote servers (which is only possible using the script or jsonp data types), ...

Tha alternative would be to have jQuery request a local file which would receive the data server-side and serve them to the ajax request..

for example
php: http_post_data
asp: Microsoft.XMLHTTP

like image 83
Gabriele Petrioli Avatar answered Oct 02 '22 19:10

Gabriele Petrioli


What about HTML?

dataType : "html"

http://docs.jquery.com/Specifying_the_Data_Type_for_AJAX_Requests

"html": Treat the response as HTML (plain text); included script tags are evaluated.

like image 33
Thiago Belem Avatar answered Oct 02 '22 19:10

Thiago Belem