標籤:變化 頁面 let javascrip 建立 rac doctype cep test
樣本Ajax:
<%@ 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>Test Ajax</title></head><body><div style="text-align: center;"><button onclick="loadName()">測試Ajax</button></div></body><script type="text/javascript">function loadName() {var xmlHttp;if(window.XMLHttpRequest){xmlHttp=new XMLHttpRequest();}else{//IE 5,6的支援xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}//xmlHttp.open("get", "testAjax", true);//xmlHttp.open("post", "testAjax", true);/*//get請求xmlHttp.open("get", "testAjax?name=Anner&age=24", true);xmlHttp.send();*///post請求xmlHttp.open("post", "testAjax", true);xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");xmlHttp.send("name=Anner&age=24");}</script></html>
XMLHttpRequset對象相應伺服器
onreadystatechange事件
當請求被發送到伺服器時,我們需要執行一些基於響應的任務
當readystate改變時,就會觸發onreadystatechange事件
XMLHttpRequest對象的三個重要的屬性:
1、onreadystatechange儲存函數或函數名,每當readyState屬性改變時,就調用該函數
readyState存有XMLHttpRequest的狀態,從0到4發生變化
0:請求未初始化
1:伺服器串連已經建立
2:請求已接收
3:請求處理中
4:請求已完成,且相應已就緒
status:
200:OK
404:未找到頁面
如需獲得來自伺服器的相應,使用XMLHttpRequest對象的responseText或responseXML屬性
responseText獲得字串形式的響應資料
responseXML獲得XML形式的響應資料
Ajax返回後台Servlet資料
<%@ 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>Test Ajax</title></head><body><div style="text-align: center;"><button onclick="loadName()">測試Ajax</button><input type="text" name="te" id="te"></div></body><script type="text/javascript">function loadName() {var xmlHttp;if(window.XMLHttpRequest){xmlHttp=new XMLHttpRequest();}else{//IE 5,6的支援xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}//xmlHttp.open("get", "testAjax", true);//xmlHttp.open("post", "testAjax", true);/*//get請求xmlHttp.open("get", "testAjax?name=Anner&age=24", true);xmlHttp.send();*///alert("readyState:"+xmlHttp.readyState+"status狀態"+xmlHttp.status);xmlHttp.onreadystatechange=function(){//alert("readyState:"+xmlHttp.readyState+"status狀態"+xmlHttp.status);if(xmlHttp.readyState==4 && xmlHttp.status==200){//alert(xmlHttp.responseText);document.getElementById("te").value=xmlHttp.responseText;}};//post請求xmlHttp.open("post", "testAjax", true);xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");xmlHttp.send("name=Anner&age=24");}</script></html>
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//req.setCharacterEncoding("utf-8");//String name=req.getParameter("name");//String age=req.getParameter("age");//System.out.println(name+" "+age);resp.setContentType("text/html;charset=utf-8");PrintWriter out =resp.getWriter();out.println("ajax返回");out.flush();out.close();}
Ajax非同步XMLHttpRequest對象