Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I can't get responseXML using AJAX (XMLHttpRequest)

I'm sending XMLHttpRequest using javascript (client side) trying to get responseXML from a php page that has XML content (server side). I'm having no troubles when the html page and the php page are in the same level (both in local host). The problem starts when they are not - the responseXML is always null.

The strange thing is that I'm getting this result using different browsers (chrome, firefox, opera) except IE8 which is giving me the right responseText (NOT the responseXML) but only after I "allow blocked content".

Another thing. I'm using phonegap to turn this html page (request page) into an Android application (which is my main goal) and I'm getting the same result (null response) when I test the application on my tablet. Here is the client code:

<html>
  <head>
    <title> T_d test </title>
    <script language="javascript">
      var infoPage="http://172.25.10.215/list2.php";
      // 172.25.10.215 the IP address of the server

      function test()
      {
        var xht = new XMLHttpRequest();
        xht.open("GET",infoPage,true);
        xht.send();
        xht.onreadystatechange=function() {
          if (xht.readyState==4) {
            alert(""+xht.responseXML);
            document.getElementById("id1").innerHTML="The result is : "+xht.responseText;
          }
        }
      }
    </script>
  </head>
  <body>
    <form id="form1" name="form1" method="post" action="">
      btn
      <input name="btn" type="button" id="btn" onClick="test();" value="Submit" />
    </form>
    <label id="id1"></label>
  </body>
</html>

Here is the server page code:

<?php
  header('Content-Type: text/xml');
?>
<root>
  <file>
    <filename>Test.txt</filename>
    <fileCreatingDate>15-7-2013</fileCreatingDate>
    <fileModifyDate>20-7-2013</fileModifyDate>
    <filesize>10002345</filesize>
    <filetype> Text</filetype>
  </file>
  <file>
    <filename>Test2.txt</filename>
    <fileCreatingDate>19-7-2013</fileCreatingDate>
    <fileModifyDate>20-8-2013</fileModifyDate>
    <filesize>3002345</filesize>
    <filetype> Text</filetype>
  </file>
</root>

Here is the response text I get using IE8:

The result is : Test.txt 15-7-2013 20-7-2013 10002345 Text Test2.txt 19-7-2013 20-8-2013 3002345 Text

Could you please help?? Thanks

like image 516
T-D Avatar asked Aug 25 '26 15:08

T-D


1 Answers

You can't make an AJAX request to a server other than the server your JavaScript originated from. This is because of browsers' same origin policy, which exists for security reasons.

You may be able to use a workaround, as described in this question: Making an AJAX request to another server

like image 180
user428517 Avatar answered Aug 27 '26 04:08

user428517