1,Xmlhttp是一种浏览器对象, 可用于模拟http的GET和POST请求。配合JavaScript可以实现页面数据在无刷新下的定时​​数据更新​​,如果应用在聊天室、文字直播上可以取得较好的视觉效果。

 

2,在IE中XmlHttp被实现为ActiveX对象,通常使用var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");也可以使用var xmlhttp = createobject("MiCROSOFT.XMLHTTP") 来创建一个对象,然后使用该对象的open方法来发出一个Http请求。

 

3.代码例子:

var strURL = admin;

function xmlhttpPost(cell,strURL,action) {
var xmlHttpReq = false;
var self = this;
if (window.XMLHttpRequest) {
self.xmlHttpReq = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
self.xmlHttpReq.open('POST', strURL, true);//会进入对应的servlet
self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
self.xmlHttpReq.onreadystatechange = function() {
if (self.xmlHttpReq.readyState == 4 ) {
if(self.xmlHttpReq.status==200){
// updatepage(self.xmlHttpReq.responseText,responsediv);
var responseMsg = self.xmlHttpReq.responseText;
responseEvent(cell,responseMsg);
}else{
alert('Ajax Request Error.');
}
}

}
self.xmlHttpReq.send(action);
}

 

application/x-www-form-urlencoded:键值对形式提交

 当action为get时候,浏览器用x-www-form-urlencoded的编码方式把form数据转换成一个字串(name1=value1&name2=value2...),然后把这个字串append到url后面,用?分割,加载这个新的url。 当action为post时候,浏览器把form数据封装到http body中,然后发送到server。 

// 请求的状态有5个值:0=未初始化;1=正在加载;2=已经加载;3=交互中;4=完成;
if (xmlHttpReq.readyState == 4) {
// 200对应OK,如404=未找到网页
if (xmlHttpReq.status == 200) {
// alert(xmlHttpReq.responseText);
}
}