ajax

 
// readyState
// 0: 请求未初始化
// 1: 服务器连接已建立
// 2: 请求已接收
// 3: 请求处理中
// 4: 请求已完成,且响应已就绪
function ajax() {
var xmlhttp;
if (window.XMLHttpRequest) {
// IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
xmlhttp=new XMLHttpRequest();
}
else
{
// IE6, IE5 浏览器执行代码
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
console.log(xmlhttp.readyState)   
console.log(xmlhttp.status)

 

if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
}
}
// method:(GET / POST),url:地址,async: true(异步), false(同步)
xmlhttp.open("POST","/try/ajax/demo_post2.php",true);
// setRequestHeader(header,value)  向请求添加 HTTP 头。
// header: 规定头的名称
// value: 规定头的值

 

xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("fname=Henry&lname=Ford");
};
ajax();
 
posted @ 2019-05-07 21:05  牛牛牛牛牛牛牛牛  阅读(147)  评论(0编辑  收藏  举报