本文实例讲述了javascript中AJAX用法。分享给大家供大家参考。具体分析如下:
兼容地获得XMLHttpRequest对象:
var xhr = null;
if(window.XMLHttpRequest){ //非IE浏览器
xhr = window.XMLHttpRequest;
}else if(window.ActiveXObject){ //IE浏览器
try{ //高版本,受msxml3.dll+支持
xhr = new ActiveXObject(\"Msxml2.XMLHTTP\");
}catch(e){
try{ // 低版本,msxml2.6以下版本使用
xhr = new ActiveXObject(\"Microsoft.XMLHTTP\");
}catch(e){
alert(\"IE浏览器无法创建ActiveXObject对象!\");
}
}
}
AJAX处理函数:
xhr.open(\"POST\",url,true);
xhr.setRequestHeader(\"Content-Type\",\"application/x-www-form-urlencoded\");
xhr.onreadystatechange=stateChangeHandler;
xhr.send(); //var name=\"clf\"; xhr.send(name);
function stateChangeHandler(){
if(xhr.readystate==4&&xhr.status==200){
var obj = document.getElementById(\"targetDiv\");
obj.innerHTML = xhr.responseText;
}
}
希望本文所述对大家的javascript程序设计有所帮助。
本文地址:https://www.stayed.cn/item/25096
转载请注明出处。
本站部分内容来源于网络,如侵犯到您的权益,请 联系我