码迷,mamicode.com
首页 > Web开发 > 详细

AJAX XMLxmlHttpuest 对象

时间:2015-01-26 17:22:41      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:html   jquery   jquery ajax   ajax   



 XMLxmlHttpuest 对象

    AJAX 的要点是 XMLxmlHttpuest 对象。

   使用 XMLxmlHttpuest 对象对象实现异步通信一般需要以下几个步骤:

(1)定义 XMLxmlHttpuest 对象实例

(2)调用open()方法建立与服务器的连接

(3)注册onreadystatechange事件处理函数,以便接收和处理从服务器端响应的信息

(4)调用send()方法发送请求

(1)实例化XMLxmlHttpuest 对象

    不同的浏览器创建 XMLxmlHttpuest 对象的方法是有差异的。 IE 浏览器使用 ActiveXObject,而其他的浏览器使用名为 XMLxmlHttpuest 的 JavaScript 内建对象。

    try{
     // Firefox, Opera 8.0+, Safari   等非IE浏览器  

   XMLHttpRequest  xmlHttp=new XMLHttpRequest();  }

     catch (e){
     // Internet Explorer    IE浏览器

      try{
         xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");      }

     catch (e){    try{
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");         }

     catch (e){
             alert("您的浏览器不支持AJAX!");             

             return false;     }      }  }

(2)建立连接

    定义了XMLHttpRequest对象后,调用open()方法可以建立异步连接:

xmlHttp.open(method,url,async,user,password)

          method:请求的方式GET/POST/PUT/PROPFIND一般都使用(GET/POST)     ②打开请求,装载数据 
          url:即请求的地址,可以为绝对地址,也可以为相对地址   
          async:为可选项,设置是否为异步通信(true or  false)

          user和password表示请求的服务器需要进行验证

         例如:xmlHttp.open("GET","test.asp","true");


        xmlHttp.open("GET",url,true);     
        xmlHttp.onreadystatechange=dowork;(dowork是一个JS)    
        xmlHttp.send(null);

(3)绑定onreadystatechange时间处理函数

xmlHttp.  onreadystatechange=response;

function response(){

////可通过xmlHttp.readyState属性值跟踪通信状态或xmlhttp.responseBody,responseStream,responseText,responseXML等属性来存储服务器端不同的响应信息

readyState属性值:

0:请求未初始化(还没有调用 open())。  
1:请求已经建立,但是还没有发送(还没有调用 send())

2:请求已发送,正在处理中(通常现在可以从响应中获取内容头)。     

3:请求在处理中;通常响应中已有部分数据可用了,但是服务器还没有完成响应的生成。     

4:响应已完成;您可以获取并使用服务器的响应了

(4)通过send()方法来发送请求

 xmlHttp.send(null);

一个完整的通信为:

  XMLHttpRequest  xmlHttp=new XMLHttpRequest(); 

  xmlHttp.open();  建立连接

  xmlHttp.onreadystatechange=response; 绑定onreadystatechange事件 

  xmlHttp.send(null); 发送请求

  alert(xmlHttp.response.responseText);//提示服务器端相应信息

AJAX XMLxmlHttpuest 对象

标签:html   jquery   jquery ajax   ajax   

原文地址:http://blog.csdn.net/u012975706/article/details/43152581

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!