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; // 定義變量,用來創(chuàng)建xmlhttprequest對象,用于存放XMLHttpRequest2 createXMLHttpRequest(); // 調(diào)用創(chuàng)建對象的方法3 xmlhttp.onreadystatchange = callback; 注冊回調(diào)函數(shù);onreadystatchagne 每個狀態(tài)改變時都會觸發(fā)事件處理器,通常會調(diào)用javascript函數(shù)
4 xmlhttp.open('GET','AJAX?name='userName,true); //open方法幾個重要的參數(shù):get/post,服務器地址,
//XMLhttpRequest對象的交互方式即同步/異步,true表示異步方式)使用open方法設置和服務器交互的基本信息
5 xmlhttp.send(null); //設置發(fā)送的數(shù)據(jù),開始和服務器進行交互
if(xmlhttp.readyState==4){
//表示和服務器端的交互已經(jīng)完成
if(xmlhttp.status==200){
//表示服務器的響應代碼是200,正確的返回了數(shù)據(jù)
//純文本數(shù)據(jù)的接受方法
varmessage=xmlhttp.responsText();
//如果使用的是DOM對象的接受方法,則
//vardoxXml=xmlhttp.responseXML();
//但是有一個前提,服務器端需要設置content-type為text/xml
vardiv=document.getElementById("頁面div的ID")
div.innerHTML=message;
}
}
}
在回調(diào)函數(shù)中判斷交互是否結(jié)束,響應是否正確,并根據(jù)需要獲取服務器端返回的數(shù)據(jù),更新頁面內(nèi)容
問題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>
總結(jié)
以上是生活随笔為你收集整理的xmlhttprequest用法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: win7添加usb3.0驱动(错误代码1
- 下一篇: 【ClassCastException】