//创建Ajax对象(兼容处理) function createXHR() { var xhr = null; if(window.XMLHttpRequest) { xhr = new XMLHttpRequest(); } else if(window.ActiveXObject) { xhr = new ActiveXObject(‘Microsoft.XMLHTTP‘); } return xhr; } //发送post请求 function request(){ xhr.open(‘POST‘,‘checkRequire.php‘,true); xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");//post请求必须加上这行,get请求不需要 xhr.onreadystatechange = function() { if(this.readyState == 4 && this.status==200) { alert(this.responseTex);//返回的数据,并处理 } } xhr.send(‘name=tom&age=20‘); //发送请求数据,get请求xhr.send(null); }
时间: 2024-10-04 02:36:08