xmlhttp应用

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);
        }
    }

参考:
1.http://blog.csdn.net/main_xtgjfge/article/details/18960409

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值