zoukankan      html  css  js  c++  java
  • AJAX-----06XMLHttpRequest对象的处理返回的XML类型数据

    我们首先要确保XML文件是否正常执行

    如果访问时候报类似  Parse error: syntax error, unexpected 'version' (T_STRING  这样的错误,我们则需要修改php的配置文件:

    将这里改成Off然后从其apache即可。。

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
        <script>
            function createXHR(){
                var xhr = null;
                if(window.XMLHttpRequest){
                    xhr = new XMLHttpRequest();
                }else if(window.ActiveXObject){
                    xhr = new ActiveXObject("Microsoft.XMLHTTP");
                }
                return xhr;
            }
    
            function test1(){
                var xhr = createXHR();
                xhr.open('GET','7.php',true);
                xhr.onreadystatechange = function(){
                    if(this.readyState == 4){
                        //针对xml,ajax也给出了一种特殊的方法进行接收responseXML
                        //alert(this.responseXML);
                        var xmldom = this.responseXML;
                        var chs = xmldom.getElementsByTagName('name')[0];
                        //alert(chs.firstChild.firstChild.wholeText);
                        //alert(chs.lastChild.firstChild.wholeText);
                        document.getElementById('mpname').innerHTML = chs.firstChild.firstChild.wholeText;
                        document.getElementById('mpdesc').innerHTML = chs.lastChild.firstChild.wholeText;
                    }
                }
                xhr.send(null);
            }
        </script>
    </head>
    <body>
        <div>
            <h1>获奖名单</h1>
            <p>歌名:<span id="mpname"></span></p>
            <p>简介:<span id="mpdesc"></span></p>
        </div>
        <input type="button" value="显示获奖歌曲" onclick="test1();">
        <input type="button" value="text2" onclick="test2();">
    </body>
    </html>

    <?php
        header("Content-Type:text/xml");
    ?>
    <?xml version='1.0' encoding='UTF-8'?>
    <mp3><name bid="a666"><title>忘情水</title><desc>忘了就好</desc></name></mp3>
  • 相关阅读:
    UVA-Matrix Chain Multiplication(栈)
    UVA-514 Rails (栈)
    Apple Catching(dp)
    Curling 2.0(dfs回溯)
    River Hopscotch(二分最大化最小值)
    The Longest Straight(二分,离散化)
    2015福建省赛
    N bulbs(规律)
    杭电校赛(虐哭。。。)
    【数字图像处理】灰度直方图、直方图均衡化、直方图规定化
  • 原文地址:https://www.cnblogs.com/leigood/p/6038734.html
Copyright © 2011-2022 走看看