开发者

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

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 :(


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


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.


It's called same origin policy. You are not allowed to use the Ajax calls to fetch data from other domains unless you are using either the jsonp or script data types.


This should do the trick:

$(document).ready(function(){ 

$('#test').click( function() {
    $.ajax({ 
    url : 'http://soiduplaan.tallinn.ee/',
    dataType : 'xml',  
    data : {a : 'p.routes', transport_id : 'tram', t : 'xml', l : 'ee'}, 
    error : function(xhr, stat){ 
    alert('error');  
    }, 
    success : function(data){ 
    alert('success'); 
    $('#foo').html(data);  // could also use text(data) to see XML returned as text
    } 
    }); 
  });
});  

Assuming you have a HTML file like:

<body>
<div>
<h1> Tester</h1>
</div>
<br />
     <input type="button" id="test" value="Test" />
     <div id="foo">
     </div>
 </body>

NOTE: This appears to work fine in IE8 but not so much in FF3.6


Doh'

tried YQL query only to find out that robots.txt is blocking :(

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜