第一步:创建XMLHttpRequest对象
var httpxml ;
if(window.XMLHttpRequest){
//大多数浏览器
httpxml = new XMLHttpRequest();
}else{
//古董级浏览器
httpxml = new ActiveXObject("Microsoft.XMLHTTP");
}
第二步:向服务器发送请求
使用 XMLHttpRequest 对象的 open() 和 send() 方法。具体参数,如下所示:
使用方法:
- GET使用方法:
xmlhttp.open(“GET”,“URL”,true);
xmlhttp.send(); - POST使用方法
xmlhttp.open(“POST”,“URL”,true);
xmlhttp.send();
如果 post 请求中带参数需使用setRequestHeader() 来添加 HTTP 头。
xmlhttp.open(“post”,“url”,true);
xmlhttp.setRequestHeader(“Content-type”,“application/x-www-form-urlencoded”);
第三步:使用 onreadystatechange 事件监听状态变化,并获取服务器响应
xmlhttp.onreadystatechange = function()