Asynchronous JavaScript and XML

RIA的應用,Rich Internet Application,富互聯(lián)網(wǎng)應用,使BS擁有CS的優(yōu)點。

XHR,XmlHTTPRequest

ajax原生代碼

        var ajaxbtn=document.getElementById("ajaxbtn");
        ajaxbtn.onblur=function() {
        var xhr;        //1、創(chuàng)建xhr對象。
        if (window.XMLHttpRequest) {     //兼容IE
            xhr = new XMLHttpRequest();
           } else {
            xhr = new ActiveXObject("Microsoft.XMLHTTP");
           }
        xhr.open("get","/users/validate?username="+username.value);     
             //2、打開連接,此條第三個屬性為是否異步,默認為true,異步,可以不寫。
        xhr.send(null);    //3、發(fā)送請求,或不寫null,get提交的參數(shù)寫在上一行l(wèi)ogin加?后面
        xhr.onreadystatechange=function(){    //通過事件回調來處理服務器返回的數(shù)據(jù)
            if(xhr.readyState==4&&xhr.status==200){    
               //4代表服務器響應完成,200帶便響應狀態(tài)碼正確
                var text=xhr.responseText;
                console.log(text);
            }
          }
        };

 


JQuery 的AJAX
load   載入遠程 HTML 文件代碼并插入至 DOM 中。

       $(":button[value=load]").click(function(){
            $("#content").load("index.html");
        });
get   通過遠程 HTTP GET 請求載入信息。

       $.get("/users/validate",{username:"abc"}, function(data){
            console.log(data)
        });
post   通過遠程 HTTP POST 請求載入信息。

       $.post("/users/login1",{username:"123",password:123}, function(data){
            console.log(data)
        });
ajax   

       $.ajax({
                    type:"post",
                    url:"/users/reg",
                    data:{
                        username:$("#zhuceusername").val(),
                        password:$("#zhucepwd").val()
                    },
                    success:function(data){
                        alert("注冊成功");
                        window.location="ajax.html"
                    }
                });