您好,登錄后才能下訂單哦!
這篇文章主要介紹了AJAX使用get與post模式有什么區(qū)別,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
具體分析如下:
如果是get 模式的請(qǐng)求,則將傳遞參數(shù)通過URL 地址發(fā)送到服務(wù)器端;
如果是post 模式的請(qǐng)求,則將傳遞參數(shù)通過send( ) 方法發(fā)送到服務(wù)器端(并且必須設(shè)置請(qǐng)求文件頭);
post 模式的代碼如下:
<script type="text/javascript"> <!-- var queryString = "firstName=xugang&birthday=1227"; var url = "9-3.aspx?timetamp=" + new Date().getTime(); xmlHttp.open("POST",url); xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); xmlHttp.send(queryString); //該語句負(fù)責(zé)發(fā)送數(shù)據(jù) //--> </script>
一個(gè)演示get 模式與post 模式區(qū)別的示例:
客戶端:
代碼示例:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>GET VS. POST</title> <script language="javascript"> var xmlHttp; function createXMLHttpRequest(){ if(window.ActiveXObject) xmlHttp = new ActiveXObject("Microsoft.XMLHttp"); else if(window.XMLHttpRequest) xmlHttp = new XMLHttpRequest(); } function createQueryString(){ var firstName = document.getElementById("firstName").value; var birthday = document.getElementById("birthday").value; var queryString = "firstName=" + firstName + "&birthday=" + birthday; return encodeURI(encodeURI(queryString)); //兩次編碼解決中文亂碼問題 } // GET 模式 function doRequestUsingGET(){ createXMLHttpRequest(); var queryString = "9-3.aspx?"; queryString += createQueryString() + "×tamp=" + new Date().getTime(); xmlHttp.onreadystatechange = handleStateChange; xmlHttp.open("GET",queryString); xmlHttp.send(null); } // POST 模式 function doRequestUsingPOST(){ createXMLHttpRequest(); var url = "9-3.aspx?timestamp=" + new Date().getTime(); var queryString = createQueryString(); xmlHttp.open("POST",url); xmlHttp.onreadystatechange = handleStateChange; xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); xmlHttp.send(queryString); } function handleStateChange(){ if(xmlHttp.readyState == 4 && xmlHttp.status == 200){ var responseDiv = document.getElementById("serverResponse"); responseDiv.innerHTML = decodeURI(xmlHttp.responseText);//解碼 } } </script> </head> <body> <h3>輸入姓名和生日</h3> <form> <input type="text" id="firstName" /><br> <input type="text" id="birthday" /> </form> <form> <input type="button" value="GET" onclick="doRequestUsingGET();" /><br> <input type="button" value="POST" onclick="doRequestUsingPOST();" /> </form> <div id="serverResponse"></div> </body> </html>
服務(wù)器端
代碼示例:
<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="gb2312" %> <%@ Import Namespace="System.Data" %> <% if(Request.HttpMethod == "POST") Response.Write("POST: " + Request["firstName"] + ", your birthday is " + Request["birthday"]); else if(Request.HttpMethod == "GET") Response.Write("GET: " + Request["firstName"] + ", your birthday is " + Request["birthday"]); %>
通常在數(shù)據(jù)不多,并且不敏感的時(shí)候,使用get 模式的請(qǐng)求;
而數(shù)據(jù)量大,或者數(shù)據(jù)敏感的時(shí)候,使用post 模式的請(qǐng)求。
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“AJAX使用get與post模式有什么區(qū)別”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。