本文主要為大家分析了Ajax技術組成原理,供大家參考,具體內容如下
1、Ajax
特點:局部刷新、提高用戶的體驗度,數據從服務器商加載
2、AJax的技術組成
不是新技術,而是之前技術的整合
Ajax: Asynchronous Javascript And Xml;(異步的JavaScript和XML)
包括的技術:JavaScript、XML、CSS、XMLHttpRequest
異步:發送請求以后,不等結果,由回調函數處理。
JavaScript:向服務器發送請求,獲得返回結果,更新頁面
XML:用來封裝數據
3、Ajax核心原理
XMLHttpRequst對象:通過該對象向服務器發送請求。
它是異步請求的技術,所有現代瀏覽器都支持(Chrome、IE5+)
1)創建XMLHttpReuest對象
非IE瀏覽器(Mozilla/Safari):var xhr=new XMLHttpRequest();
IE:xhr=new ActiveXObject("Msxml2.XMLHTTP");
低版本IE:xhr=new ActiveXObject("Microsfot.XMLHTTP");
2)XMLHttpRequest對象的屬性與方法
a)方法:open("GET/POST",URL,true/false):用來向服務器建立連接
有三個參數:
參數1:提交方式,post或get
參數2:請求的URL
參數3:表示同步或異步請求,true:表示異步請求
false: 表示同步請求
send(data):發送請求
參數:提交的內容。
POST方式:data就是提交的參數,send(username=root&password=abc123);
GET方式:send(null)
b)屬性:
onreadystatechange:設置狀態改變時的回調函數,回調函數用來獲取服務器數據。
onreadystatechange=function(){
}
readyState:服務器狀態響應
狀態碼:
0:未初始化
1:正在加載
2:加載完成
3:請求進行中
4:請求完成
responseText:服務器返回的數據(文本格式)
responseXML:服務器返回的數據(XML格式)
總結:
使用XMLHttpRequest的步驟:
1)創建XMLHttpRequest對象
2)設置請求的方法及URL
xhr.open("GET/POST","url",true/false),true表示異步請求,false表示同步請求
3)設置狀態改變時的回調函數
xhr.onreadystatechange=function(){}
0:未初始化
1:正在加載
2:加載完成
3:請求進行中
4:請求完成
4)發送請求
xhr.send(data),
如果為post提交,則data為提交的數據,如果為get提交,則參數為null即可。
判斷用戶登錄的HTML頁面:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>登錄</title></head><body> 用戶名:<input type="text" name="username" id="username"><br> 密碼:<input type="password" name="password" id="password"> <a href="javascript:chkUser();">登錄</a> <div id="res"></div></body><script type="text/javascript"> var xhr; /** * 創建XMLHttpRequest對象 */ function createXMLHttpRequest(){ //1、創建XMLHttpRequest對象 if(window.XMLHttpRequest){ //非IE內核瀏覽器 xhr=new XMLHttpRequest(); }else{ //IE瀏覽器 try{ xhr=new ActiveXObject("Msxml2.XMLHTTP"); }catch(e){ //IE低版本 xhr=new ActiveXObject("Microsoft.XMLHTTP"); } } } /** * 發送請求,用來檢查用戶名、密碼是否正確 */ function chkUser(){ //1、創建XMLHttpRequest createXMLHttpRequest(); //2、獲得用戶名、密碼 var username=document.getElementById("username").value; var password=document.getElementById("password").value; //3、與服務器建立連接:open var url="login?username="+username+"&password="+password; //方式1:get提交 //xhr.open("GET",url,true); //方式2:post提交 var url2="login"; xhr.open("POST",url2,true); //4、設置回調函數,獲得服務器響應的數據 xhr.onreadystatechange=function(){ /* readyState狀態碼: 0:未初始化 1:正在加載 2:加載完成 3:請求進行中 4:請求完成 */ if(xhr.readyState==4){ //status,200表示響應正常 if(xhr.status==200){ //alert("從服務器返回的值為:"+xhr.responseText); var res=xhr.responseText; if(res=='0'){ document.getElementById("res").innerHTML="登錄成功"; }else{ document.getElementById("res").innerHTML="<font color='red'>登錄失敗</font>"; } }else{ alert("出現在了異常:"+xhr.response.Text); } } } //5、發送請求 //方式1:get方式 //xhr.send(null); //方式2:post方式: //Post提交需要設置http請求頭 xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xhr.send("username="+username+"&password="+password); } </script></html>
新聞熱點
疑難解答
圖片精選