function ajax(){
var xmlhttp;
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}else{
// code for IE6,IE5
xmlhttp = ActionXObject("Microsoft.XMLHTTP");
}
//判定执行状态
xmlhttp.onreadystatechange = function(){
/*
readyState
0: 请求未初始化
1: 服务器连接已建立
2: 请求已接收
3: 请求处理中
4: 请求已完成,且响应已就绪
status
200:请求成功
404:未找到
500:服务器内部错误
*/
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;//获得字符串形式的响应数据,如果返回的是XML需要单独解析
//responseXML获得 XML 形式的响应数据
var xmlDoc = xmlhttp.responseXML;
var txt = "";
var num = xmlDoc.getElementsByName("value");//获取节点name=value的值
for(var i=0;i
txt = txt+num[i].childNodes[0].nodeValue+"
";
}
document.getElementById("myDiv2").innerHTML = txt;
}
}
//@param 最后一个参数表示是否是异步提交,为了避免使用缓存我们加上一个时间戳
xmlhttp.open("Get","url"+
(function(){
var date = new Date();
return date.getSeconds();
}),true);
//设置头信息
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
//将信息发送到服务器
xmlhttp.send();
}
总结
以上是编程之家为你收集整理的手写 ajax全部内容,希望文章能够帮你解决手写 ajax所遇到的程序开发问题。
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。