0%

XMLHttpRequest GET POST与服务器交互

javascript基础学得差不多了,今天正式开始ajax,ajax最关键的当然是与服务器交互。

            var xmlHttp;
            function createXmlHttpRequest(){
                if (window.ActiveXObject) {
                    xmlHttp = new window.ActiveXObject("Microsoft.XMLHTTP");
                }
                else 
                    if (window.XMLHttpRequest) {
                        xmlHttp = new XMLHttpRequest();
                    }
            }
            createXmlHttpRequest();
            function GET(){
                xmlHttp.open("GET", "3.php", true);
                xmlHttp.onreadystatechange = function(){
                    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
                        alert(xmlHttp.responseText);
                    }
                }
                xmlHttp.send(null);
            }
            function POST(){
                xmlHttp.open("POST","3.php");
                alert("POST");
                xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
                xmlHttp.onreadystatechange = function(){
                    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
                        alert(xmlHttp.responseText);
                    }
                }
                xmlHttp.send("name=zhang");
            }