ajax学习笔记(二)——AJAX详解
2012-10-10 16:55
288 查看
第一篇,理解了ajax是个什么东西,现在我们来好好探索一下它吧!
先发一个我自己做的小程序(简易百度搜索),我们再来一步步理解它:
附加:后台处理文件processing1.ashx
View Code
request.readyState 的状态
0:请求没有发出(在调用 open() 之前)。
1:请求已经建立但还没有发出(调用 send() 之前)。 一般用不着这之前
2:请求已经发出正在处理之中(这里通常可以从响应得到内容头部)。
3:请求已经处理,响应中通常有部分数据可用,但是服务器还没有完成响应。
4:响应已完成,可以访问服务器响应并使用它
responseText 或 responseXML 属性
responseXML:后面会专门做一个xml的例子。
至此:ajax我算是入门了吧?
先发一个我自己做的小程序(简易百度搜索),我们再来一步步理解它:
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <script type="text/javascript"> var xmlHttp=false; /*@cc_on @*/ /*@if(@_jscript_version >= 5) try{ xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); }catch(e){ try{ xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){ xmlHttp=false; } } @end @*/ if(!xmlHttp && typeof XMLHttpRequest != 'undefined'){ xmlHttp=new XMLHttpRequest(); } function callServer(){ var result=document.getElementById("Search").value; if(result=="" || result==null) return; var url="documentproc/processing1.ashx?key="+escape(result); xmlHttp.open("GET",url,true); xmlHttp.onreadystatechange=updatePage; xmlHttp.send(null); } function updatePage(){ if(xmlHttp.readyState==1){ document.getElementById("stateStr").innerHTML="正在加载文章对象……"; } if(xmlHttp.readyState==2){ document.getElementById("stateStr").innerHTML="连接对象加载完毕!"; } if(xmlHttp.readyState==3){ document.getElementById("stateStr").innerHTML="数据获取中,请稍后……"; } if(xmlHttp.readyState==4){ if (xmlHttp.status==200){ var result=xmlHttp.responseText.split("_"); document.getElementById("stateStr").innerHTML=result[0]; } else if (xmlHttp.status == 404){ document.getElementById("stateStr").innerHTML="Request URL does not exist,发送的地址错了,没有此页面"; } else if (xmlHttp.status == 403){ document.getElementById("stateStr").innerHTML="Access denied.无权访问"; } else{ document.getElementById("stateStr").innerHTML="Error: status code is " + xmlhttp.status; } } } </script> <title>无标题页</title> </head> <body> <input id="Search" type="text" style="width: 200px;" onkeyup="callServer();"/><span style=" font-size:12px; color:Red">*请输入字母a-e</span> </br> <span id="stateStr" style=" background-color:Yellow; color:Black;"></span> </body> </html>
附加:后台处理文件processing1.ashx
View Code
function updatePage(){ if(xmlHttp.readyState==1){ document.getElementById("stateStr").innerHTML="正在加载文章对象……"; } if(xmlHttp.readyState==2){ document.getElementById("stateStr").innerHTML="连接对象加载完毕!"; } if(xmlHttp.readyState==3){ document.getElementById("stateStr").innerHTML="数据获取中,请稍后……"; } if(xmlHttp.readyState==4){ if (xmlHttp.status==200){ var result=xmlHttp.responseText.split("_"); document.getElementById("stateStr").innerHTML=result[0]; } else if (xmlHttp.status == 404){ document.getElementById("stateStr").innerHTML="Request URL does not exist,发送的地址错了,没有此页面"; } else if (xmlHttp.status == 403){ document.getElementById("stateStr").innerHTML="Access denied.无权访问"; } else{ document.getElementById("stateStr").innerHTML="Error: status code is " + xmlhttp.status; } } }
request.readyState 的状态
0:请求没有发出(在调用 open() 之前)。
1:请求已经建立但还没有发出(调用 send() 之前)。 一般用不着这之前
2:请求已经发出正在处理之中(这里通常可以从响应得到内容头部)。
3:请求已经处理,响应中通常有部分数据可用,但是服务器还没有完成响应。
4:响应已完成,可以访问服务器响应并使用它
responseText 或 responseXML 属性
document.getElementById("stateStr").innerHTML=xmlHttp.responseText;
responseXML:后面会专门做一个xml的例子。
至此:ajax我算是入门了吧?
相关文章推荐
- AJAX学习笔记之原生JS使用方法
- [ajax 学习笔记] ajax初试
- JQuery学习笔记 [Ajax] (6-1)
- 学习ajax笔记
- drupal6的AJAX,ahah学习研究笔记
- 【jQuery学习笔记-----AJAX之jQuery角度分析】
- JS学习笔记(三):HTML通过ajax请求获取并显示php中的内容
- PHP学习笔记(15)PDO数据库操作+AJAX无刷新技术删除用户
- ajax 学习笔记之一 w3c DOM
- Ajax基础学习笔记三—jQuery实现Ajax
- 关于学习ajax中的contentType属性笔记
- ajax开学简略(学习笔记)
- ASP.NET AJAX Extensions 学习笔记之下载安装
- AJAX学习笔记之 制作自动校验的表单
- AJAX 学习笔记--$的用法
- ASP.NET MVC4 IN ACTION学习笔记-第六波[Ajax in ASP.NET MVC][1/3]
- javascript学习笔记(七)Ajax和Http状态码
- PHP学习笔记 22 - AJAX
- jquery学习笔记-----ajax
- 学习笔记:Ajax原理