xmlhttprequest用法
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml">
<script language='javascript' type='text/javascript'>
1 var req; var xmlHttpRequest; // 定義變量,用來創建xmlhttprequest對象,用于存放XMLHttpRequest2 createXMLHttpRequest(); // 調用創建對象的方法3 xmlhttp.onreadystatchange = callback; 注冊回調函數;onreadystatchagne 每個狀態改變時都會觸發事件處理器,通常會調用javascript函數
4 xmlhttp.open('GET','AJAX?name='userName,true); //open方法幾個重要的參數:get/post,服務器地址,
//XMLhttpRequest對象的交互方式即同步/異步,true表示異步方式)使用open方法設置和服務器交互的基本信息
5 xmlhttp.send(null); //設置發送的數據,開始和服務器進行交互
if(xmlhttp.readyState==4){
//表示和服務器端的交互已經完成
if(xmlhttp.status==200){
//表示服務器的響應代碼是200,正確的返回了數據
//純文本數據的接受方法
varmessage=xmlhttp.responsText();
//如果使用的是DOM對象的接受方法,則
//vardoxXml=xmlhttp.responseXML();
//但是有一個前提,服務器端需要設置content-type為text/xml
vardiv=document.getElementById("頁面div的ID")
div.innerHTML=message;
}
}
}
在回調函數中判斷交互是否結束,響應是否正確,并根據需要獲取服務器端返回的數據,更新頁面內容
問題1.unicorn.xml取不到值.
問題2.是不是接口沒連接上
<script type='text/javascript'>
var client = new XMLHttpRequest();
function createXMLHttpRequest(){
xmlHttp.onreadystate = handlestate
};
function handler (){
xmlHTTP = new xmlhttprequest();
client.onreadystatechange = handler;
client.open("GET", "unicorn.xml",true);
client.send(unicorn.xml);
document.write (unicorn.xml)
}
</script>
總結
以上是生活随笔為你收集整理的xmlhttprequest用法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: win7添加usb3.0驱动(错误代码1
- 下一篇: 【ClassCastException】