ASP过程用于页面动态显示内容,而goforms过程用于响应用户输入、更新设置或执行待定动作。
goahead3.3.0编译版https://download.csdn.net/download/qq_40155090/86721990
1.写*.html为结尾的html标签页面:
(参考:配置不改前提下,源代码编译后,页面代码放在doc文件夹下)
例1: test.html 页面
xmlhttp.open("GET","action/test",true);---- 应用actions
<!DOCTYPE html>
<html>
<head>
<title>test.html</title>
<meta charset="UTF-8">
</head>
<body>
<p>Please log in</p>
<form action=/action/test method="post">
<table>
<tr><td>账号:</td><td><input type="text" name="name"></td></tr>
<tr><td>密码:</td><td><input type="password" name="address"></td></tr>
<tr><td><input type="submit" name="submit" value="submit"></td>
<td><input type="reset" value="reset"></td></tr>
</table>
</form>
<script type="text/javascript">
function loadXMLDoc()
{
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;
}
}
xmlhttp.open("GET","action/test",true);
xmlhttp.send();
}
</script>
<div id="myDiv"><h2>需要刷新的局部内容</h2></div>
<button type="button" onclick="loadXMLDoc()">通过 AJAX 实现局部刷新</button>
</body>
</html>
2.在auth.c文件websOpenAuth函数中注册函数
PUBLIC int websOpenAuth(int minimal)
{
char sbuf[64];
assert(minimal == 0 || minimal == 1);
if ((users = hashCreate(-1)) < 0) {
return -1;
}
if ((roles = hashCreate(-1)) < 0) {
return -1;
}
if (!minimal) {
fmt(sbuf, sizeof(sbuf), "%x:%x", rand(), time(0));
secret = websMD5(sbuf);
#if BIT_GOAHEAD_JAVASCRIPT && FUTURE
websJsDefine("can", jsCan);
#endif
websDefineAction("login", loginServiceProc);
websDefineAction("logout", logoutServiceProc);
//注册函数
websDefineAction("test", actionTest);
///
}
if (smatch(BIT_GOAHEAD_AUTH_STORE, "file")) {
verifyPassword = websVerifyPasswordFromFile;
#if BIT_HAS_PAM
} else if (smatch(BIT_GOAHEAD_AUTH_STORE, "pam")) {
verifyPassword = websVerifyPasswordFromPam;
#endif
}
return 0;
}
3.在auth.c文件中定义actionTest函数
static void actionTest(Webs *wp)
{
cchar *name, *address;
name = websGetVar(wp, "name", NULL);
address = websGetVar(wp, "address", NULL);
websSetStatus(wp, 200);
websWriteHeaders(wp, -1, 0);
websWriteEndHeaders(wp);
websWrite(wp, "<html><body><h2>name: %s, address: %s</h2></body></html>", name, address);
websFlush(wp);
websDone(wp);
}
//可在auth.c文件末尾定义函数
/*
@copy default
......
*/
4.在auth.c 文件声明actionTest函数
/********************************** Forwards **********************************/
//......
//可在Forwards下#if前声明前声明函数
//声明actionTest
static void actionTest(Webs *wp);
//
#if
//...
#endif
修改源代码后,需要 重新编译goahead。 每次修改源码内容时,都需要把libgoahead.so库更新才有效,因为goahead把其他依赖的文件做成了动态库。
说明:1."action/test" 实际上是路由:http://ip:port/action/test
2.访问test页面:http://ip:port/test.html
3.启动服务:./goahead -v --home /etc/test /var/doc ip:port
4.页面都放在doc目录下
sudo ./goahead -v --home PATH1 PATH2
-v : 打开运行日志
--home : 指定运行目录, 第一个参数 PATH1 是配置文件的所在目录 第二个参数 PATH2 是web页面所在目录
doc //帮助文档
src //源码
test //测试示例