ajax异步请求

<%@ page language="java" pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
        <title></title>
        <script type="text/javascript">
            var request = false;
            try {
                request = new XMLHttpRequest();
            }catch(trymicrosoft) {
                try {
                    request = new ActiveXObject("Msxml2.XMLHTTP");
                }catch(othermicrosoft) {
                    try {
                        request = new ActiveXObject("Microsoft.XMLHTTP");
                    }catch(failed) {
                        request = false;
                    }
                }
            }
            if(!request) {
                alert("Error initializing XMLHttpRequest!");
            }
            function getCustomerInfo() {
                var phone = document.getElementById("phone").value;
                var url = "test.jsp";
                request.open("GET",url,true);
                request.onreadystatechange = updatePage;
                request.send(null);
            }
            function updatePage() {
                if(request.readyState == 4) {
                    if(request.status == 200) {
                        //var datas = request.responseXML.getElementsByTagName("data");
                        var datas = request.responseXML.documentElement;
                        var da = datas.childNodes.item(0).text;
                        //var response = request.responseText;
                        var test = document.getElementById("tt");
                        test.innerText = da;
                    }
                }
            }
        </script>
    </head>
    <body>
        <form action="POST">
        <p>Enter your phone number:
        <input type="text" size="14" name="phone" id="phone"
        onChange="getCustomerInfo();" />
        </p>
            <div id="tt"></div>
        </form>
    </body>
</html>

 

test.jsp

<%response.setContentType("text/xml");%><?xml version='1.0' encoding='gb2312' ?>
<home>
    <data>Zjcfan</data>
</home>

 

<?xml version='1.0' encoding='gb2312' ?>前面一定要注意,不能有任何空格和回车。否则XML样式错误,用FireFox来测试,IE不报错

你可能感兴趣的:(Ajax,jsp,xml,IE,firefox)