2017最新javascript中AJAX用法
javascript中AJAX用法
兼容地獲得XMLHttpRequest對(duì)象:
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瀏覽器無(wú)法創(chuàng)建ActiveXObject對(duì)象!"); } } }
AJAX處理函數(shù):
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; } }