AJAX核心(XMLHttpRequest)
其实AJAX就是在Javascript中多添加了一个对象:XMLHttpRequest对象。所有的异步交互都是使用XMLHttpServlet对象完成的。也就是说,我们只需要学习一个Javascript的新对象即可。
1 | var xmlHttp = new XMLHttpRequest();(大多数浏览器都支持DOM2规范) |
注意,各个浏览器对XMLHttpRequest的支持也是不同的!为了处理浏览器兼容问题,给出下面方法来创建XMLHttpRequest对象:
function createXMLHttpRequest() { ???????var xmlHttp; ???????// 适用于大多数浏览器,以及IE7和IE更高版本 ???????try{ ???????????xmlHttp = new XMLHttpRequest(); ???????} catch (e) { ???????????// 适用于IE6 ???????????try { ???????????????xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); ???????????} catch (e) { ???????????????// 适用于IE5.5,以及IE更早版本 ???????????????try{ ???????????????????xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); ???????????????} catch (e){} ???????????} ???????} ???????????????????return xmlHttp; ???}
使用流程
步骤1: 打开与服务器的连接(open方法)
当得到XMLHttpRequest对象后,就可以调用该对象的open()方法打开与服务器的连接了。open()方法的参数如下:
open(method, url, async):
- method:请求方式,通常为GET或POST;
- url:请求的服务器地址,例如:/ajaxdemo1/AServlet,若为GET请求,还可以在URL后追加参数;
- async:这个参数可以不给,默认值为true,表示异步请求;
1 2 | var xmlHttp = createXMLHttpRequest(); xmlHttp.open( "GET" , "/ajax_get/?a=1" , true ); |
步骤2: 发送请求
当使用open打开连接后,就可以调用XMLHttpRequest对象的send()方法发送请求了。send()方法的参数为POST请求参数,即对应HTTP协议的请求体内容,若是GET请求,需要在URL后连接参数。
注意:若没有参数,需要给出null为参数!若不给出null为参数,可能会导致FireFox浏览器不能正常发送请求!
1 | xmlHttp.send( null ); |
步骤3: 接收服务器响应
当请求发送出去后,服务器端就开始执行了,但服务器端的响应还没有接收到。接下来我们来接收服务器的响应。
XMLHttpRequest对象有一个onreadystatechange事件,它会在XMLHttpRequest对象的状态发生变化时被调用。下面介绍一下XMLHttpRequest对象的5种状态:
- 0:初始化未完成状态,只是创建了XMLHttpRequest对象,还未调用open()方法;
- 1:请求已开始,open()方法已调用,但还没调用send()方法;
- 2:请求发送完成状态,send()方法已调用;
- 3:开始读取服务器响应;
- 4:读取服务器响应结束。
onreadystatechange事件会在状态为1、2、3、4时引发。
下面代码会被执行四次!对应XMLHttpRequest的四种状态!
xmlHttp.onreadystatechange = function() { ???????????alert(‘hello‘); ???????};
但通常我们只关心最后一种状态,即读取服务器响应结束时,客户端才会做出改变。我们可以通过XMLHttpRequest对象的readyState属性来得到XMLHttpRequest对象的状态。
xmlHttp.onreadystatechange = function() { ???????????if(xmlHttp.readyState == 4) { ???????????????alert(‘hello‘); ???????????????} ???????};
其实我们还要关心服务器响应的状态码是否为200,其服务器响应为404,或500,那么就表示请求失败了。我们可以通过XMLHttpRequest对象的status属性得到服务器的状态码。
最后,我们还需要获取到服务器响应的内容,可以通过XMLHttpRequest对象的responseText得到服务器响应内容。
xmlHttp.onreadystatechange = function() { ???????????if(xmlHttp.readyState == 4 && xmlHttp.status == 200) { ???????????????alert(xmlHttp.responseText); ???????????????} ???????};
if 发送POST请求
<1>需要设置请求头:xmlHttp.setRequestHeader(“Content-Type”, “application/x-www-form-urlencoded”);注意 :form表单会默认这个键值对不设定,Web服务器会忽略请求体的内容。
<2>在发送时可以指定请求体了:xmlHttp.send(“username=yuan&password=123”)
JS实现ajax小结
/* ???创建XMLHttpRequest对象; ???调用open()方法打开与服务器的连接; ???调用send()方法发送请求; ???为XMLHttpRequest对象指定onreadystatechange事件函数,这个函数会在 ???XMLHttpRequest的1、2、3、4,四种状态时被调用; ???XMLHttpRequest对象的5种状态,通常我们只关心4状态。 ???XMLHttpRequest对象的status属性表示服务器状态码,它只有在readyState为4时才能获取到。 ???XMLHttpRequest对象的responseText属性表示服务器响应内容,它只有在 ???readyState为4时才能获取到!*/
var ele_btn=document.getElementsByTagName("button")[0] ???ele_btn.onclick=function () { ???????// 发送ajax ????????// (1) 获取 XMLHttpRequest对象 ????????xmlHttp = new XMLHttpRequest(); ????????// ?(2) 连接服务器 ???????// ?get ???????//xmlHttp.open("get","/sendAjax/?a=1&b=2"); ???????// ?post ???????xmlHttp.open("post","/sendAjax/"); ???????// 设置请求头的Content-Type ???????xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); ???????// (3) 发送数据 ???????var ele_csrf=document.getElementsByName("csrfmiddlewaretoken")[0]; ???????ele_num1=document.getElementsByClassName("num1")[0]; ???????ele_num2=document.getElementsByClassName("num2")[0]; ???????ele_ret=document.getElementsByClassName("ret")[0]; ???????s1="num1="+ele_num1.value; ???????s2="num2="+ele_num2.value; ???????s3="&csrfmiddlewaretoken="+ele_csrf.value; ???????xmlHttp.send(s1+"&"+s2+s3) ; ??// 请求体数据 ???????// (4) 回调函数 ?success ???????xmlHttp.onreadystatechange = function() { ???????????if(this.readyState==4 && this.status==200){ ???????????????ele_ret.value=this.responseText ???????????} ???????}; ???}
var ele_btn=document.getElementsByTagName("button")[0] ???ele_btn.onclick=function () { ???????// 发送ajax ????????// (1) 获取 XMLHttpRequest对象 ????????xmlHttp = new XMLHttpRequest(); ????????// ?(2) 连接服务器 ???????// ?get ???????//xmlHttp.open("get","/sendAjax/?a=1&b=2"); ???????// ?post ???????xmlHttp.open("post","/sendAjax/"); ???????// 设置请求头的Content-Type ???????var ele_csrf=document.getElementsByName("csrfmiddlewaretoken")[0]; ???????xmlHttp.setRequestHeader("Content-Type","application/json"); ???????xmlHttp.setRequestHeader("X-CSRFToken",ele_csrf.value); ???????// (3) 发送数据 ???????xmlHttp.send(‘{"name":"moses","age":22}‘) ; ??// 请求体数据 ???????// (4) 回调函数 ?success ???????xmlHttp.onreadystatechange = function() { ???????????if(this.readyState==4 && this.status==200){ ???????????????console.log(this.responseText) ???????????} ???????}; ???}
用JS实现Ajax请求
原文地址:http://www.cnblogs.com/wxp5257/p/7929452.html