利用 xmlhttp 實現無刷新傳輸數據
參考:
[ http://www.knowsky.com/5190.html ]
示例代碼:test.htm
<html>
<head>
<title>利用 xmlhttp 實現無刷新傳輸數據</title>
</head>
<script language=javascript>
<!--
function showOnDiv(){
var xh = new ActiveXObject("MSXML2.XMLHTTP");
xh.open("get","http://www.google.com",false);
xh.setRequestHeader("Content-Type","gb2312");
xh.send();
if(xh.readyState == 4){
alert(xh.responseText);
resultDiv.innerHTML = xh.responseText;
}
}
//-->
</script>
<body>
<form id="form1">
<input type="button" value="div顯示" onclick="showOnDiv();" ID="Button1" NAME="Button1">
</form>
<div id="resultDiv"></div>
</body>
</html>
======================
說明:
1。open(method, url, async, userID, passwork) 方法
前三個參數是必選的,后兩個是可選的(服務器端身份驗證)
method:http 通信方式,"post " 或 "get"
url:接收請求的服務器的URL,可帶參數
async:異步(true),同步(false)
2。send(obj)
obj:發送到服務器的數據,可以是字符串,DOM樹,數據流…
3。readyState 屬性
反映服務器處理請求的進展狀況:
0:Response對象已經創建,但XML文檔上載過程尚未結束
1:XML文檔已經裝載完畢
2:XML文檔已經裝載完畢,正在處理中
3:部分XML文檔已經解析
4:文檔已經解析完畢,客戶端可以接受返回消息
4。接收響應是通過XMLHTTP對象的屬性實現的:
● responseTxt:將返回消息作為文本字符串;
● responseXML:將返回消息視為XML文檔,在服務器響應消息中含有XML數據時使用;
● responseStream:將返回消息視為Stream對象。
待解決問題:中文亂碼