AJAX XMLxmlHttpuest 对象
??
XMLxmlHttpuest 对象 AJAX 的要点是 XMLxmlHttpuest 对象。 使用XMLxmlHttpuest 对象对象实现异步通信一般需要以下几个步骤: (1)定义XMLxmlHttpuest 对象实例 (2)调用open()方法建立与服务器的连接 (3)注册onreadystatechange事件处理函数,以便接收和处理从服务器端响应的信息 (4)调用send()方法发送请求 (1)实例化XMLxmlHttpuest 对象 不同的浏览器创建 XMLxmlHttpuest 对象的方法是有差异的。 IE 浏览器使用 ActiveXObject,而其他的浏览器使用名为 XMLxmlHttpuest 的 JavaScript 内建对象。 try{ XMLHttpRequestxmlHttp=new XMLHttpRequest(); } catch (e){ try{ catch (e){ try{ catch (e){ return false; } } } (2)建立连接 定义了XMLHttpRequest对象后,调用open()方法可以建立异步连接: xmlHttp.open(method,url,async,user,password) method:请求的方式GET/POST/PUT/PROPFIND一般都使用(GET/POST) ②打开请求,装载数据 user和password表示请求的服务器需要进行验证 例如:xmlHttp.open("GET","test.asp","true");
(3)绑定onreadystatechange时间处理函数 xmlHttp.onreadystatechange=response; function response(){ ////可通过xmlHttp.readyState属性值跟踪通信状态或xmlhttp.responseBody,responseStream,responseText,responseXML等属性来存储服务器端不同的响应信息 readyState属性值: 0:请求未初始化(还没有调用 open())。 2:请求已发送,正在处理中(通常现在可以从响应中获取内容头)。 3:请求在处理中;通常响应中已有部分数据可用了,但是服务器还没有完成响应的生成。 4:响应已完成;您可以获取并使用服务器的响应了 } (4)通过send()方法来发送请求 xmlHttp.send(null); 一个完整的通信为: XMLHttpRequestxmlHttp=new XMLHttpRequest(); xmlHttp.open(); 建立连接 xmlHttp.onreadystatechange=response; 绑定onreadystatechange事件 xmlHttp.send(null); 发送请求 alert(xmlHttp.response.responseText);//提示服务器端相应信息 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |