Ajax非同步XMLHttpRequest對象

來源:互聯網
上載者:User

標籤:變化   頁面   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對象

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.