views:

1715

answers:

4

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

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'.

Kobi
thats it. i also recognized: the calling html with js has sto be on localhost!
stephan
A: 

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>
seth
Updated my answer for you.
seth
A: 

Hi there,

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

bjoernwibben
my main problem was that the calling html with the javascript wasn't on the server... i just double clicked on it. while the html is within the localhost root all ajax stuff works! thx to all
stephan
+1  A: 

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

Gorilla3D
it is! :-) thx for reply
stephan