3

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

cbascom
  • 766
  • 1
  • 5
  • 21
T-D
  • 1,306
  • 1
  • 16
  • 23

4 Answers4

3

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

Community
  • 1
  • 1
user428517
  • 4,132
  • 1
  • 22
  • 39
  • If this is the case , then why IE8 is giving me the response ? is it because I chose to allow blocked content which might be a part of the mentioned policy ? if that's true why other browsers don't give me this option ? – T-D Aug 06 '13 at 20:53
0

Your XML is not valid. You're missing this line as the first line of your XML document:

<?xml version="1.0" ?> 
Fotiman
  • 111
  • 7
0

CORS could be the issue here. To verify try testing the same in google chrome with "--disable-web-security" enabled. This will disable web-security and enable CORS. needless to say, that do not use this mode while normal internet surfing.

PS: you will need to close all chrome windows/tab and launch a fresh one with the security disabled for it to work.

v2b
  • 1,436
  • 9
  • 15
0

Added to this...

Your Open should be

xht.open("GET","infopage.extension",true); 

Your button onclick event should be onClick="test()" not with ;

Tobia Zambon
  • 7,479
  • 3
  • 37
  • 69
SayMaxil
  • 27
  • 8