AJAX原始的GET和POST请求方式


//创建XMLHttpRequest对象
var xmlHttpRequest;
function AjaxXMLHttpRequest(){
//获得页面数据
var username = document.getElementById("username").value;
if(window.XMLHttpRequest){
//Firefox IE7 IE8 Opera
xmlHttpRequest = new XMLHttpRequest();
if(xmlHttpRequest.overrideMineType){
//修正一些浏览器的BUG
xmlHttpRequest.overrideMineType("text/xml");
}
}else if(window.ActiveXObject){
//IE6 为什么这个写在后面?因为前面那个好,用更新的
activexName = ["MSXML2.XMLHTTP","Microsoft.XMLHTTP"];
for(var i=0;i<activexName.length;i++){
try{
//一次一次的尝试创建xmlHttpRequest对象
xmlHttpRequest = new ActiveXObject(activexName[i]);
}catch(e){

}
}
}
//设置回调函数
xmlHttpRequest.onreadystatechange = callbacks;
//第一个参数表示HTTP请求的方式
//第二个参数表示get请求的连结
//第三个表示同步还是异步,true表示是异步
xmlHttpRequest.open("get","servlet/aykjaservlet?username=" + encodeURI(username),true);
//同步方式下要在这里占停,等待数据发送回来
xmlHttpRequest.send(null);

/**以下是POST方式
xmlHttpRequest.onreadystatechange = callbacks;//回调函数
xmlHttpRequest.open("POST","ProcessServlet",true);//指定POST方式提交
xmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); //设置头信息
xmlHttp.send("username="+encodeURI(username)); //发送的时候传的参数
*/
}
function callbacks(){
//判断对象交互状态是否完成,完成了等于4
//一定这个值发生变化就会调用这个回调函数
if(xmlHttpRequest.readyState == 4){
//判断HTTP交互是完成,完成了等于200
//404表示没有找到
if(xmlHttpRequest.status == 200){
//返回的值xml和text
var responseText = xmlHttpRequest.responseText;
$("#result").html(responseText);
/**
以下是从服务器获得XML
var domObj = xmlHttpRequest.responseXML;//获得DOM对象
var messageNode = domObj.getElementsByTagName("message");//获得节点
var textNode = messageNode[0].firstChild;//获得文本结点
$("#result").html(textNode.nodeValue);//取值,并显示到HTML中

服务器端一定要设置
response.setContentType("text/xml;charset=utf-8");
*/

}
}
}


[color=blue]综上,只有是POST方法时,才能传递URL+参数外的数据,XHR明显是考虑到了这一点。当send的数据存在时,自动使用POST。
http://www.iteye.com/topic/221344

XMLHttpRequest详解
http://www.iteye.com/topic/199990[/color]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值