无为清净楼资源网 Design By www.qnjia.com
本文实例讲述了AJAX简单异步通信的方法。分享给大家供大家参考。具体分析如下:
客户端:向服务器发出一个空请求。
代码如下:
<html> <head> <title>XMLHttpRequest</title> <script language="javascript"> var xmlHttp; function createXMLHttpRequest(){ if(window.ActiveXObject) xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); else if(window.XMLHttpRequest) xmlHttp = new XMLHttpRequest(); } function startRequest(){ createXMLHttpRequest(); xmlHttp.open("GET","9-1.aspx",true); xmlHttp.onreadystatechange = function(){ if(xmlHttp.readyState == 4 && xmlHttp.status == 200) alert("服务器返回: " + xmlHttp.responseText); } xmlHttp.send(null); } </script> </head> <body> <input type="button" value="测试异步通讯" onClick="startRequest()"> </body> </html>
服务器端:向客户端直接返回一个字符串。
代码如下:
复制代码 代码如下:<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="gb2312" %>
<%@ Import Namespace="System.Data" %>
<%
Response.Write("异步测试成功,很高兴");
%>
问题一:
由于IE 浏览器会自动缓存异步通信的结果,不会实时更新服务器的返回结果。(但Firefox 会正常刷新)
为了解决异步连接服务器时IE 的缓存问题,更改客户端代码如下:
var sUrl = "9-1.aspx" + new Date().getTime(); //地址不断的变化 xmlHttp.open("GET",sUrl,true);
在访问的服务器地址末尾添加一个当前时间的毫秒数参数,使得每次请求的URL地址不一样,从而欺骗IE 浏览器来解决IE 缓存导致的更新问题。
问题二:
当测试程序时,如果客户端和服务器端都在同一台计算机上时,异步对象返回当前请求的http状态码 status == 0 ,于是再次更改客户端代码如下:
//if(xmlHttp.readyState == 4 && xmlHttp.status == 200) if( xmlhttp.readyState == 4) { if( xmlhttp.status == 200 || //status==200 表示成功! xmlhttp.status == 0 ) //本机测试时,status可能为0。 alert("服务器返回: " + xmlHttp.responseText); }
于是,最终的客户端代码如下:
<html> <head> <title>XMLHttpRequest</title> <script language="javascript"> var xmlHttp; function createXMLHttpRequest(){ if(window.ActiveXObject) xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); else if(window.XMLHttpRequest) xmlHttp = new XMLHttpRequest(); } function startRequest(){ createXMLHttpRequest(); var sUrl = "9-1.aspx" + new Date().getTime(); //地址不断的变化 xmlHttp.open("GET",sUrl,true); xmlHttp.onreadystatechange = function(){ //if(xmlHttp.readyState == 4 && xmlHttp.status == 200) if( xmlhttp.readyState == 4) { if( xmlhttp.status == 200 || //status==200 表示成功! xmlhttp.status == 0) //本机测试时,status可能为0。 alert("服务器返回: " + xmlHttp.responseText); } } xmlHttp.send(null); } </script> </head> <body> <input type="button" value="测试异步通讯" onClick="startRequest()"> </body> </html>
希望本文所述对大家的Ajax程序设计有所帮助。
标签:
AJAX,异步通信
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
更新日志
2024年09月25日
2024年09月25日
- 纯音入心系列纯音乐《天籁古筝》1CD[MP3][331MB]
- 男女对唱典藏天碟《发烧对唱·那个季节里的歌DSD》10CD[WAV]
- 群星2010-歌林精选辑[歌林][WAV+CUE]
- TrioZimbalist-PianoTriosofWeinberg,Auerbach,Dvorak(2024)[24-44,1]wav
- 群星.1992-滚石第一流台湾歌·12王牌大车拼【滚石】【WAV+CUE】
- 林子祥.1984-创作歌集【华纳】【WAV+CUE】
- 周汤豪.2010-周汤豪【华纳】【FLAC分轨】
- Mozart-TheStringQuintets-AmadeusQuartet,CecilAronowitz(2017)[24-44,1][WAV+CUE]
- JamesWilliamsDennisIrwin-Focus(2024,Red)[24-48]FLAC
- 藤泽麻衣《空みあげて》[WAV]
- 纯音入心系列纯音乐《中国古筝经典名曲》1CD[MP3][1.3GB]
- 纯音入心系列纯音乐《古筝新奏:岁月静好与筝语,细水流年与筝同》1CD[MP3][846.9MB]
- 670《脆弱敏感小女生》[320K/MP3][27.53MB]
- 曾琳.2011-好好爱我【南方】【WAV+CUE】
- 范晓萱.1995-RAIN【福茂】【WAV+CUE】