一、異步請求的get請求的完整五步曲瀏覽器
//1.0建立一個異步對象 var xhr = new XMLHttpRequest(); //2.0打開鏈接 // 請求方式 請求路徑 是否異步 xhr.open("get", "/P02Time.ashx", true); //3.2設置請求報文頭(清除緩存) xhr.setRequestHeader("If-Modified-Since", 0);//瀏覽器設置爲當前請求不緩存 //4.0設置回調函數(約定數據響應回來之後操做) xhr.onreadystatechange = function () { if (xhr.readyState == 4 && xhr.status == 200) { //獲得從服務器拿回來的數據 var text = xhr.responseText; document.getElementById("div").innerHTML = text; } } //5.0發送請求 xhr.send();
二、異步請求的的post請求的完整五步曲緩存
//1.0建立對象 var xhr = new XMLHttpRequest(); //2.0打開鏈接 xhr.open("post", "/P02Time.ashx", true); //3.0設置請求報文頭 xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); //4.0設置回調函數 xhr.onreadystatechange = function () { if (xhr.readyState == 4 && xhr.status == 200) { var text = xhr.responseText; document.getElementById("div").innerHTML = text; } } //5.0發送請求 xhr.send("id=1&name=2");