天天看點

基于原生ajax前後資料互動post和get的簡單執行個體get和post的差別

get和post的差別

說明:無論是用ajax還是form表單來實作互動,首先我們要明确get和post的使用場合

get是限制長度的,post不會

get請求資料大小不超過2000字元左右,post無限制

get請求的查詢字元串顯示在位址欄,post不會,即是post更安全

本文實作功能

通過ajax的get或post方法,發送一個查詢字元串uname=lisi&upwd=123456請求,并響應回來結果,資訊正确與否;

common.js檔案

common.js檔案,簡化一些dom操作和複雜固定步驟

function $(id) {

return document.getElementById(id);

} //簡化document.getElementById()這個api

function createXhr(){//簡化建立異步對象

var xhr=null;

if(window.XMLHttpRequest==null){

xhr=new ActiveXObject(“Microsoft.XMLHttp”);

}else{

xhr=new XMLHttpRequest();

}

return xhr;

}

ajax.php

用于接收網頁傳過來的請求資料,并傳回結

繼續閱讀