duannao8450 2013-07-07 23:25
浏览 55
已采纳

解析简单的XML - Javascript / Ajax

I want to dynamically change page content based on XML Ajax response. For testing I have created a static PHP generated XML file with only one tag. If the tag contains the string "yes" then it should display "AVAIL" otherwise display something else. The static value set for testing is "yes" but the script still displays "Not Avail".

What is the correct way to accomplish this?

I have some AJAX here..

xmlhttpp.onreadystatechange=function(){
if(xmlhttpp.readyState==4 && xmlhttpp.status==200){
    var response = xmlhttpp.responseXML;
    var avail = response.getElementsByTagName("avail")[0];
    if(avail.childNodes[0].nodeValue == "yes"){
        document.getElementById("dstat").innerHTML = "AVAIL";
    }else{
        document.getElementById("dstat").innerHTML = "NOT AVAIL '" + avail + "'";
    }


}

}

And the domain_checker.php file looks like this..

<?php
Header('Content-type: text/xml');
echo "<?xml version='1.0' encoding='UTF-8'?>
<domain>
  <avail>yes</avail>
</domain>";
?>
  • 写回答

1条回答 默认 最新

  • dqnek0079 2013-07-07 23:33
    关注

    avail is an xml node, what you want is the text inside the node to test against

        avail = response.getElementsByTagName("avail")[0];
        if(avail.childNodes[0].nodeValue == "yes"){
            document.getElementById("dstat").innerHTML = "AVAIL";
        }else{
            document.getElementById("dstat").innerHTML = "NOT AVAIL";
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境
  • ¥30 关于#java#的问题,请各位专家解答!
  • ¥30 vue+element根据数据循环生成多个table,如何实现最后一列 平均分合并
  • ¥20 pcf8563时钟芯片不启振