weixin_33691700 2012-04-19 18:00 采纳率: 0%
浏览 52

通过Ajax从服务器检索XML文件,但为什么无法将其解析?

我试图通过Ajax从服务器检索XML文件,但无法将其解析,我不知道是哪出了问题?当我调用getFriends.php时,它会打印XML。以下是Ajax代码:

<!DOCTYPE>
<html>
<head>
<title>Ajax Sample</title>

<script type="text/javascript">

function getFriendsList(MemberId) {

    var xmlhttp;

    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
    }
    else {
        // code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange=function() {

    if (xmlhttp.readyState==4 && xmlhttp.status==200) {

            var xmlDoc = xmlhttp.responseXML;
            var friendElements = xmlDoc.getElementsByTagName('friend');

            for (var x=0; x<friendElements.length; x++) {
                // We know that the first child of show is title, and the second is rating
                var id = showElements[x].childNodes[0].value;
                var name = showElements[x].childNodes[1].value;

                // Now do whatever you want with the show title and ratings
                document.write("hi");
          }

        }
    }

    xmlhttp.open("POST","getFriends.php",true);
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xmlhttp.send("id=" + MemberId);

}

</script>

</head>

<body>

    <input name="ajax" type="button" onClick="getFriendsList(1)" value="Click for AJAX">
    <div style="background-color:#00FF99;" id="placehere">Here is where the update will occur.</div>
</body>
</html>

这是getFriends.php代码(用户类运行正常):

<?php
include('lib.php');
//$id = $_POST['id'];
$id=1;
$user = new User($id);

echo $user->getFriendsList();

?>
  • 写回答

2条回答 默认 最新

  • weixin_33676492 2012-04-19 18:07
    关注

    using jquery to perform ajax calls is easy and hassle free see http://api.jquery.com/jQuery.ajax/ for more info

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!