博客列表 >Ajax基础

Ajax基础

廖磊的博客
廖磊的博客原创
2017年08月02日 23:30:03715浏览

                                                                                          Ajax

一、创建ajax的对象,然后让对象来调用他本身的成员本身的属性

1.主流(火狐、Google、苹果、opera)浏览器

Var xhr= new XMLHttpRequest();

2.IE(6/7/8)浏览器创建

Var xhr=new ActiveXObject(“Microsoft.XHttp”); //最原始的方式
Var xhr=new ActiveXObject(“Msxml2.XMLHTTP.6.0”);

套路总结:

if(window.ActiveXObject){
Var xhr=new ActiveXObject(“Microsoft.XHttp”);
}
Var xhr= new XMLHttpRequest();

二、创建http协议请求

xhr.open(method,URL,flag);
Method: post / get
URL:请求的URL地址
Flag:true(异步交互) false(同步交互) 
Eg:  
① xhr.open("get","ajax.php",true);
xhr.send(null); //发送http协议请求
 
② xhr.open(“post”,”ajax.php”,true);
Xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
Xhr.send(data); //发送http协议请求

四、最大程度感受状态信息

xhr.onreadystatechange=function () {
   if (xhr.readyState==4  &&  xhr.status==200){
        //alert(xhr.responseText);
        document.getElementsByTagName("div")[0].innerHTML = xhr.responseText;
    }
}


声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议