【问题标题】:xml validity, ajax, and phpxml 有效性、ajax 和 php
【发布时间】:2009-07-12 17:32:33
【问题描述】:

我通过 ajax 将一些 XML 从 PHP 返回到 Javascript,并得到一些“无效的 xml”错误。我返回的 xml 如下所示:

<response>
<song>tdb2009-01-29s2s06</song>
<song>tdb2009-01-29s1s02</song>
</response>

我的 javascript 解析它看起来像:

 function u_handleServerResponse(){  
   //pull xml from xml response  
   var xmlResponse = xmlHttp.responseXML;

   //check to see if xml was pulled
   if(!xmlResponse || !xmlResponse.documentElement){
     throw("Invalid XML Structure:\n" + xmlHttp.responseText);
   }

   //this is for catching errors with firefox
   var rootNodeName = xmlResponse.documentElement.nodeName;

   //check for errors
   if(rootNodeName == "parsererror"){
     throw("Invalid XML Strucutre");
   }

   //get the root
   xmlRoot = xmlResponse.documentElement;

   var songArray = xmlRoot.getElementsByTagName("song");

  for(var i = 0; i < songArray.length; i++){   
   etc., etc...

我得到了一个

读取响应时出错:无效的 XML 结构

错误。你觉得这一切都对吗? xml是错误的还是加载错误?非常感谢所有帮助。提前谢谢...

【问题讨论】:

    标签: php xml ajax


    【解决方案1】:

    如果您仍有问题,请查看这篇文章:
    https://web.archive.org/web/1/http://articles.techrepublic%2ecom%2ecom/5100-10878_11-6141415.html

    问候,
    布鲁诺

    【讨论】:

      【解决方案2】:

      您可能需要包含一个 xml 标头...

      <?xml version="1.0" ?> 
      

      【讨论】:

      • 你知道我如何在 PHP 中做到这一点(我正在构建 xml)吗?
      【解决方案3】:

      除了 rikh 指出的 XML 标头之外,您可能需要将 Content-Type 标头声明为 text/xml 才能正确填充 responseXML。

      【讨论】:

      • 我在创建并返回 xml 的 php 文件的开头这样做。我需要在其他地方做吗?
      猜你喜欢
      • 2017-05-16
      • 2016-12-13
      • 2011-12-12
      • 2020-11-21
      • 1970-01-01
      • 2014-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多