jquery ajax call not working?! firefox or xss prob

2019-05-19 03:47发布

问题:

My problem is: in firefox i got no response. in ie it worked fine. I want a ajax call to a local script getting some information in plain text or something else. but it won't work. I think cross scripting should not a problem at this point or?

the javascript code is simple:

var targetUrl = "http://localhost/jQueryProxy.php";
var parameters = ""; // later

$.ajax({    
  type: "GET",
  async: true,
  url: targetUrl,
  data: parameters,
  success: function(param1, param2){
    alert(param1);
  }
});

and the php code too:

<?php
   header('Content-type: text/xml'));
   echo "test";
?>

回答1:

try var targetUrl = "/jQueryProxy.php";
Also, you can check Firefox's javascript console to look for an error: Ctrl+Shift+J

You can also try looking for jQuery's Ajax error message, by adding an handler (source):

error:function (xhr, ajaxOptions, thrownError){
    alert(xhr.status);
    alert(xhr.statusText);
    alert(thrownError);
 }  

Update: I've done some testing, is seems Firefox blocks Ajax from local files to the web (localhost too, for that matter), but does not throw an exception. Using alert($('*', param1).text()); at success shows the content of the current document, which is weird.
Placing the XML as a local file doesn't work either, the behavior of FF and IE is inconsistency - they act very differently.
Your best bet is to place the html on the server (localhost), on the same port as your xml file (80 here).
Also, when your xml is valid, consider adding dataType:'xml'.



回答2:

It's because the content type is text/xml but it's not valid XML.

If you want it to be XML, change the echo to be:

<?xml version="1.0" encoding="UTF-8"?>
<foo>test</foo>


回答3:

Its an XSS problem. Generally local html pages are much more sandboxed then public html pages.



回答4:

this seems to work on FF:

var targetUrl = "http://localhost/jQueryProxy.php";
var parameters = ""; // later

$.ajax({    
  type: "GET",
  async: true,
  url: targetUrl,
  data: parameters,
  success: function(param1, param2){
    alert($(param1).find("foo").text());
  }
});

and php looks like that:

<?php
   header('Content-type: text/xml');
   echo '<?xml version="1.0" encoding="UTF-8"?><foo>test</foo>';
?>

best regards,

bjoern