操作系统:CentOS7
Web服务器:BOA
语言:HTML+JS+C
演示了网页如何把数据通过AJAX发给后台CGI和后台CGI如何把数据返回到网页的指定控件显示
源码参考:AJAX_CGI
BOA的搭建参考:Linux下嵌入式Web服务器BOA和CGI编程开发
小案例实战参考:嵌入式web服务器BOA+CGI+HTML+MySQL项目实战——Linux
实现效果:在对应输入框内输入内容调用对应函数,非空,就显示cgi程序返回的内容
if(strstr(lenstr,"txtIDA")!=NULL)
{
printf("[email protected]\n\n");
}
if(strstr(lenstr,"txtIDB")!=NULL)
{
printf("Liuyixu\n\n");
}
然后给对应控件进行赋值
document.getElementById("txtIDB").innerHTML=xmlhttp.responseText;
<input type="submit" value="get_info" onclick="get_info()" />
实现获取cgi发送回来的json字符串
if(strstr(lenstr,"get_info") != NULL)
{
printf("{\"A\":\"%s\",\"B\":\"%s\"}",ip,prot);
}
转json对象,给对应控件赋值的效果
//将接收到的字符串存入jsonstr
var jsonstr = xmlhttp.responseText;
alert("json:"+jsonstr);
// 将jsonstr转换为json对象 json
var json = JSON.parse(jsonstr);
if(json[name1]){
var value = json[name1];
// 直接给id为name1的控件赋值
document.getElementById(name1).innerHTML = value;
}
if(json[name2]){
var value = json[name2];
document.getElementById(name2).innerHTML = value;
}