我无法使用AJAX(XMLHttpRequest)获得responseXML

时间:2022-12-07 00:17:12

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.

我正在使用javascript(客户端)发送XMLHttpRequest,试图从具有XML内容(服务器端)的php页面获取responseXML。当html页面和php页面处于同一级别时(在本地主机中),我没有麻烦。问题在它们不存在时开始 - responseXML始终为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".

奇怪的是,我使用不同的浏览器(chrome,firefox,opera)得到这个结果,除了IE8,它给了我正确的responseText(不是responseXML),但只有在我“允许阻止的内容”之后。

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:

另一件事。我正在使用phonegap将这个html页面(请求页面)转换为Android应用程序(这是我的主要目标),当我在平板电脑上测试应用程序时,我得到了相同的结果(空响应)。这是客户端代码:

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

这是我使用IE8获得的响应文本:

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

结果是: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

能否请你帮忙??谢谢

4 个解决方案

#1


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.

您不能向JavaScript源自服务器以外的服务器发出AJAX请求。这是因为浏览器的原始策略相同,出于安全原因存在。

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

您可以使用解决方法,如此问题中所述:向另一台服务器发出AJAX请求

#2


0  

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

您的XML无效。您缺少此行作为XML文档的第一行:

<?xml version="1.0" ?> 

#3


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.

CORS可能是这里的问题。要验证尝试在Google Chrome中测试相同内容,并启用“--disable-web-security”。这将禁用Web安全性并启用CORS。不用说,在正常的网上冲浪时不要使用这种模式。

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

PS:你需要关闭所有镀铬窗口/标签并启动一个新的安全禁用安全窗口才能工作。

#4


0  

Added to this...

添加到...

Your Open should be

你的公开赛应该是

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

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

您的按钮onclick事件应该是onClick =“test()”而不是;

#1


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.

您不能向JavaScript源自服务器以外的服务器发出AJAX请求。这是因为浏览器的原始策略相同,出于安全原因存在。

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

您可以使用解决方法,如此问题中所述:向另一台服务器发出AJAX请求

#2


0  

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

您的XML无效。您缺少此行作为XML文档的第一行:

<?xml version="1.0" ?> 

#3


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.

CORS可能是这里的问题。要验证尝试在Google Chrome中测试相同内容,并启用“--disable-web-security”。这将禁用Web安全性并启用CORS。不用说,在正常的网上冲浪时不要使用这种模式。

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

PS:你需要关闭所有镀铬窗口/标签并启动一个新的安全禁用安全窗口才能工作。

#4


0  

Added to this...

添加到...

Your Open should be

你的公开赛应该是

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

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

您的按钮onclick事件应该是onClick =“test()”而不是;