物件的建立:
var xhr = null;
if(window.xmlhttprequest)else
或者使用try catch語句var xhr = null;
trycatch(e)
responsetext中的內容格式是字串
window.onload = function()else
trycatch(e)
xhr.open('get','1.txt',true);中的內容是[1,2,3,4,5]
xhr.send();
xhr.onreadystatechange = function()
}} }
post方式:
window.onload = function() catch (e)
xhr.open('post','2.post.php',true);
//post方式,資料放在send()裡面作為引數傳遞
//post沒有快取問題
//無需編碼
xhr.send('username=劉偉&age=30');
xhr.onreadystatechange = function() else }}
}}
get方式:
window.onload = function() catch (e)
/*1.快取 在url?後面連線乙個隨機數,時間戳
2.亂碼 編碼encodeuri
*/xhr.open('get','2.get.php?username='+encodeuri('劉偉')+'&age=30&' + new date().gettime(),true);
xhr.send();
xhr.onreadystatechange = function() else }}
}}
get和post方式區別:
get有快取,post沒有;
get引數放在位址列,post引數放在send()函式中;
get存在編碼問題,post要設定請求頭
Ajax學習筆記
send string string 僅用於post請求 使用get還是post 與post相比,get更簡單更快,大部分情況下使用 下面情況使用post請求 無法使用快取檔案 更新伺服器上的檔案或資料庫 向伺服器傳送大量資料 傳送包含未知字元的使用者輸入時,post更穩定和可靠 get請求例項 請...
ajax學習筆記
ajax學習 1 ajax的作用 ajax區域性重新整理技術,其實就是使用js實現區域性資料的實時變更 解決了不同響應之間資料共享的問題 優勢 提高了網路傳輸效率,提公升了使用者體驗 2 ajax的使用 建立ajax物件 瀏覽器差異問題 複寫onreadystatechange函式 接收響應結果 響...
AJAX學習筆記
http狀態碼 1xx 資訊類,表示收到web瀏覽器請求,正在進一步的處理中 2xx 成功,表示使用者請求被正確接收,理解和處理,例如200 ok 3xx 重定向,表示請求沒有成功,客戶必須採取進一步的動作 4xx 客戶端錯誤,表示客戶端提交的請求有錯誤,例如404 not found,意味著請求中...