【问题标题】:return xml in jquery ajax在 jquery ajax 中返回 xml
【发布时间】:2011-08-28 11:14:37
【问题描述】:

我的问题是我想将一个 xml 文件从服务器返回给客户端,并使用 jquery 的 ajax 函数对其进行解析。这是代码:

客户:

$("#submit").click(function(){          
    $.ajax({  
        type: "POST",  
        url: "search.php",  
        data: "whatever",
        dataType: "xml",
        async: false,
        success: function(xml){
            var data = $('doctor',xml).text();
            alert(data);
        }
    });
});

服务器(php 文件),

header('Content-type: text/xml');
echo '<?xml version="1.0" encoding="utf-8"?>';
echo "<tables>";
echo "<doctor>Someone</doctor>";
echo "</tables>";

我有一个空白警报,我不知道为什么??


好的,我找到了。我的 php 文件是这种形式
//some code
include("other.php");
//some other code

other.php 文件是我在上面发布的文件。我剪切/粘贴标题,所以最终的 php 文件将是

//some code
header('Content-type: text/xml');
include("other.php");
//some other code

和其他.php

echo '<?xml version="1.0" encoding="utf-8"?>';
echo "<tables>";
echo "<doctor>Someone</doctor>";
echo "</tables>";

现在它完美运行。感谢您的快速回复!

【问题讨论】:

  • $(xml).find("doctor") 有用吗?

标签: jquery xml ajax types return


【解决方案1】:

一切正常

Post.php 文件

if($_GET['id']!=""){    
    $array = array('satyam'  => 'satyam',
                   'class'   => 'B.TECH',
                   'company' => 'Ranosys');
}   

$new ='<?xml version="1.0" encoding="iso-8859-1"?><data>';
foreach($array as $key => $values){ 
    $new .= "<$key>$values</$key>";
}
echo $new.'</data>';

=================

function load_data(){
    $.ajax({
        url: "post.php",
        async: false, // stop browser for another activity
        data: "id=satyam",
        // dataType :'xml',
        error: function(e, b, error) { 
            for(var i in e){
              // alert(i);
            }
            alert(e.respone);
        },
        success: function(msg) {
            //alert($response);
            var data = $(msg).find("satyam").text();
            alert(data);
        }
    });
}

【讨论】:

    【解决方案2】:

    试试这个:var data = $(xml).find('doctor').text()

    在您的示例中,“xml”不是 jQuery 对象。

    【讨论】:

    【解决方案3】:

    你需要解析这个XML(我真的不明白为什么,但是......),你可以通过do来完成:

    $(xml).find('doctor').text();
    

    再见。 :)

    【讨论】:

      【解决方案4】:

      您必须将您的功能更改为:

      $("#submit").click(function(){       
          $.ajax({  
              type: "POST",  
              url: "search.php",  
              data: "whatever",
              dataType: "xml",
              async: false,
              success: function(xml){
      
                  var xmlDoc;
      
                  if (window.DOMParser) {
                      parser = new DOMParser();
                      xmlDoc = parser.parseFromString(xml, "text/xml");
                  }
                  else {// Internet Explorer
                      xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
                      xmlDoc.async = "false";
                      xmlDoc.loadXML(xml);
                  }
                  var $response = $(xmlDoc);
                  var data = $response.find("doctor").text()
      
                  alert(data);
              }
          });
      });
      

      if (window.DOMParser) { 的原因是您在 IE 进行解析时会遇到问题。

      【讨论】:

      • 根本不工作。我在 Firefox 4 上运行该应用程序。我尝试了 IE9,但它根本不显示警报。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-08
      • 1970-01-01
      • 2012-11-07
      • 1970-01-01
      相关资源
      最近更新 更多