在appweb中使用javascript和在其它服务器中使用时类似,这里简单记录,方便以后查看
这里的工程是基于博客http://blog.csdn.net/xiaominthere/article/details/38756421中的工程
1.页面
在client文件夹下建立一个js文件夹,将jquery.js文件拷贝到js文件夹下
修改index.esp
每隔1s就向服务器发起获取服务器时间的请求,显示到页面
<html> <head> <title>MyJS</title> <script type="text/javascript" src="js/jquery.js"></script> <script type="text/javascript"> function getTime() { var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, 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; } } setInterval(function() { xmlhttp.open("GET","/do/login/time",true); xmlhttp.send(); }, 1000); } </script> </head> <body onLoad="getTime()"> <div id="myDiv"></div> </body> </html>2.响应
修改login.c
/* login Controller for esp-html-mvc (esp-html-mvc) */ #include "esp.h" #include <time.h> static void checkLogin() { cchar *name = param("name"); cchar *pwd = param("pwd"); if(smatch("abc",name) && smatch("123",pwd)) { renderView("login/login-ok"); }else{ renderView("login/login-error"); } } static void getTime() { time_t timep; time (&timep); char *time = ctime(&timep); HttpConn *conn = getConn(); HttpQueue *q = conn->writeq; httpSetStatus(conn, 200); httpWrite(q, time); httpFinalize(conn); } static void common(HttpConn *conn) { } /* Dynamic module initialization */ ESP_EXPORT int esp_controller_mylogin_login(HttpRoute *route, MprModule *module) { espDefineBase(route, common); espDefineAction(route, "login-cmd-check", checkLogin); espDefineAction(route, "login-cmd-time", getTime); return 0; }3. 编译测试