Ajax发送post请求

 

//创建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(){
     var xhr = createXHR();
	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);
}

  

posted @ 2014-07-15 14:01  自学it技术  阅读(162)  评论(0编辑  收藏  举报