Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我能';t使用AJAX(XMLHttpRequest)获取responseXML_Php_Javascript_Xml_Ajax_Cordova - Fatal编程技术网

Php 我能';t使用AJAX(XMLHttpRequest)获取responseXML

Php 我能';t使用AJAX(XMLHttpRequest)获取responseXML,php,javascript,xml,ajax,cordova,Php,Javascript,Xml,Ajax,Cordova,我使用javascript(客户端)发送XMLHttpRequest,试图从包含XML内容的php页面(服务器端)获取responseXML。当html页面和php页面处于同一级别(都在本地主机中)时,我没有遇到任何问题。当它们不是时问题就开始了-responseXML总是空的 奇怪的是,我使用不同的浏览器(chrome、firefox、opera)得到这个结果,除了IE8,IE8给了我正确的responseText(不是responseXML),但只有在我“允许阻止的内容”之后 还有一件事。我

我使用javascript(客户端)发送XMLHttpRequest,试图从包含XML内容的php页面(服务器端)获取responseXML。当html页面和php页面处于同一级别(都在本地主机中)时,我没有遇到任何问题。当它们不是时问题就开始了-responseXML总是空的

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

还有一件事。我使用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>

T检验
var infoPage=”http://172.25.10.215/list2.php";
//172.25.10.215服务器的IP地址
功能测试()
{
var xht=new XMLHttpRequest();
xht.open(“获取”,infoPage,true);
xht.send();
xht.onreadystatechange=函数(){
if(xht.readyState==4){
警报(“+xht.responseXML”);
document.getElementById(“id1”).innerHTML=“结果是:”+xht.responseText;
}
}
}
btn
以下是服务器页面代码:

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

Test.txt
15-7-2013
20-7-2013
10002345
正文
Test2.txt
19-7-2013
20-8-2013
3002345
正文
以下是我使用IE8获得的响应文本:

结果是:Test.txt 15-7-2013 20-7-2013 10002345 Text Test2.txt 19-7-2013 20-8-2013 3002345 Text

你能帮忙吗??
谢谢

除了您的JavaScript源服务器之外,您不能向其他服务器发出AJAX请求。这是因为浏览器的存在是出于安全原因


您可以使用解决方法,如本问题中所述:

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

<?xml version="1.0" ?> 


CORS可能是这里的问题。要验证,请尝试在启用“-disable web security”的情况下在google chrome中测试相同的功能。这将禁用web安全并启用CORS。不用说,在正常上网时不要使用这种模式

附言: 您需要关闭所有的chrome窗口/选项卡,并启动一个新的、禁用安全性的窗口以使其正常工作。

添加到该窗口中

你的空位应该是

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

按钮onclick事件应该是
onclick=“test()”
而不是with

所有文件都在同一台服务器上吗?您可能还需要检查
xht.status
是否为
200
如果是这种情况,那么为什么IE8会给我响应?是因为我选择允许被阻止的内容,这可能是上述策略的一部分吗?如果这是真的,为什么其他浏览器不给我这个选项?