主要是看到网上的一篇博客http://blog.csdn.net/fdemon/article/details/6358520#quote,照个这篇博客确实现不了正确的结果。现修改以下:html
1. 创建一个Html网页文件。文件名:test.htmllinux
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <title>C+CGI+Ajax在S3C2440中的应用</title> <script language="JavaScript" src="xmlhttpreq.js"></script> </head> <body> <h3>获取服务器当前时间</h3> <p>服务器当前时间是:<div id="current_time"></div></p> <input type="button" value="提交" onclick="sender()" /> </body> </html> 浏览器 |
2. 创建一个Javascript脚本文件,这个文件实现了Ajax进行异步访问服务器。文件名:xmlhttpreq.js缓存
/* *建立异步访问对象 */ function createXHR() { var xhr; 服务器 try { xhr = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xhr = new ActiveXObject("Microsoft.XMLHTTP"); } catch(E) { xhr = false; } }
if (!xhr && typeof XMLHttpRequest != 'undefined') { xhr = new XMLHttpRequest(); }
return xhr; }
/* *异步访问提交处理 */ function sender() { xhr = createXHR();
if(xhr) { xhr.onreadystatechange=callbackFunction; 网络 //test.cgi后面跟个cur_time参数是为了防止Ajax页面缓存 xhr.open("GET", "cgi-bin/test.cgi?cur_time=" + new Date().getTime()); xhr.send(null); } else { //XMLHttpRequest对象建立失败 alert("浏览器不支持,请更换浏览器!"); } }
/* *异步回调函数处理 */ function callbackFunction() { if (xhr.readyState == 4) { if (xhr.status == 200) { var returnValue = xhr.responseText;
if(returnValue != null && returnValue.length > 0) { document.getElementById("current_time").innerHTML = returnValue; } else { alert("结果为空!"); } } else { alert("页面出现异常!"); } } } 异步 |
3. 创建服务器端应用程序。文件名:test.c函数
#include <stdio.h> #include <stdlib.h> #include <time.h>
int main(void) { time_t current; struct tm *timeinfo; time(¤t); timeinfo = localtime(¤t); //这一句必定要加,不然异步访问会出现页面异常 printf("Content type: text/html\n\n");
printf("%s", asctime(timeinfo)); } ui |
在Fedora 9的终端命令行编译test.c,生成test.cgi文件,以下:spa
arm-linux-gcc -o test.cgi test.c
4. 将test.html、xmlhttpreq.js两个文件下载到开发板的www目录中。(注:www目录是boa服务器的文档根目录。你能够在开发板的/etc/boa/boa.conf文件的DocumentRoot节点配置成其余的目录,那么这三个文件就下载到你配置的目录中)。test.cgi放在cgi-bin目录中这个目录是boa的cgi配置目录,请先确实这个目录下的cgi可以执行。
5. 把开发板链接上网络,在PC机上打开浏览器并输入:http://192.168.1.230/test.html(注:这个IP地址是你的开发板的IP,还有这个网段要跟PC的网段一致),运行效果,如图:
点击提交按钮后,就会异步访问服务器端cgi程序,获取服务器端的系统时间。能够看到按钮在提交后浏览器并无刷新就取回系统时间。运行效果图以下:
以上是复制了网友的部份内容基础上改过来的。