From: http://www.jb51.cc/article/p-mxupsgwc-bam.html
ajax
“Asynchronous JavaScript and XML”(异步JavaScript和XML)
我们操作网页时往往只需要刷新网页上的一部分数据甚至可能是一个文本框内的数据,但是采用传统的刷新方式服务器会把整个页面重新发送至浏览器,浏览器再加载整个页面,这样不仅浪费了带宽,而且整个页面刷新视觉上也不流畅。
ajax技术解决了这一问题,ajax的思路是我需要刷新局部数据时给服务器一个请求,服务器收到请求后将数据将需要刷新的数据回送,浏览器接受到数据后通过脚本更新相应位置的数据,这个过程必须是在后台进行的。实现这个过程的核心便是JavaScript对象XmlHttpRequest。该对象在是一种支持异步请求的技术。简而言之,XmlHttpRequest使您可以使用JavaScript向服务器提出请求并处理响应,而不阻塞用户。
在goahead中的实现:
- <html>
- <head>
- <scripttype="text/javascript">
- functionloadXMLDoc()
- {
- varxmlhttp;
- if(window.XMLHttpRequest)
- {//codeforIE7+,Firefox,Chrome,Opera,Safari
- xmlhttp=newXMLHttpRequest();
- }
- else
- {//codeforIE6,IE5
- xmlhttp=newActiveXObject("Microsoft.XMLHTTP");
- }
- xmlhttp.onreadystatechange=function()
- {
- if(xmlhttp.readyState==4&&xmlhttp.status==200)
- {
- document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
- }
- }
- xmlhttp.open("GET","/ajax/",true);
- xmlhttp.send();
- }
- </script>
- </head>
- <body>
- <divid="myDiv"><h2>需要刷新的局部内容</h2></div>
- <buttontype="button"onclick="loadXMLDoc()">通过AJAX实现局部刷新</button>
- </body>
- </html>
<html> <head> <script type="text/javascript"> function loadXMLDoc() { var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+,Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6,IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("myDiv").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET",true); xmlhttp.send(); } </script> </head> <body> <div id="myDiv"><h2>需要刷新的局部内容</h2></div> <button type="button" onclick="loadXMLDoc()">通过 AJAX 实现局部刷新</button> </body> </html>把改htm放入服务器中。
2.在服务器端实现XMLHttpRequest的请求应答。
为ajax请求专门创建一个handle
- intwebsAjaxHandler(webs_twp,char_t*urlPrefix,char_t*webDir,intarg,
- char_t*url,char_t*path,char_t*query)
- {
- websHeader(wp);
- websWrite(wp,T("<body><h2>thisisajaxtest!</h2>\n"));
- websFooter(wp);
- websDone(wp,200);
- return1;
- }
int websAjaxHandler(webs_t wp,char_t *urlPrefix,char_t *webDir,int arg,char_t *url,char_t *path,char_t *query) { websHeader(wp); websWrite(wp,T("<body><h2>this is ajax test!</h2>\n")); websFooter(wp); websDone(wp,200); return 1; }注册handle
- websUrlHandlerDefine(T("/ajax"),NULL,websAjaxHandler,0);
websUrlHandlerDefine(T("/ajax"),0);这样就实现了局部刷新。
扩展
- xmlhttp.open("GET",true);
- xmlhttp.send();
xmlhttp.open("GET",true); xmlhttp.send();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。