如何使javascript与C中的cgi脚本通信?例如,尝试将递增的 int 值从 CGI 发送到 javascript 并在浏览器中显示。在客户端是隐藏 iframe 与 cgi 脚本的 get 方法:
<<html><head><title>Javascript cgi</title>
<style type="text/css">
#hiddenframe { display:none; }
</style>
<script type="text/javascript">
var value = 0;
function init() {
document.getElementById('thevalue').innerHTML = value;
setInterval("get_value()", 1000);
}
function get_value() {
value = thescript.value;
document.getElementById('thevalue').innerHTML = value;
thescript.location.href = 'http://localhost/homecgi/cgi.exe';
}
onload = init;
</script>
</head>
<body>
<div id="hiddenframe">
<iframe src="http://localhost/homecgi/cgi.exe" name="thescript"></iframe>
</div>
<div id="thevalue"></div>
</body>
</html>
在 C 中的服务器端 cgi 上:
int main()
{
int j=100;
char *data_get;
char *method;
printf("%s%c%c\n", "Content-Type:text/html;charset=iso-8859-1",13,10);
printf(" <html><title>hello</title><body> \n");
method = getenv("REQUEST_METHOD");
data_get = getenv("QUERY_STRING");
if( !strcmp(method, "GET") ){
printf("<br>Method %s\n", method);
printf("<br> data_get %s\n", data_get);
printf("<script>");
printf(" var value = %d ", j++);
printf("</script>");
}
else if(!strcmp(getenv("REQUEST_METHOD"), "POST")){
}
printf("<p><a href=http://localhost/homecgi/index.html >Back</a> </p>");
printf(" </body></html>");
return 0;
}
这种方式只有 j=100 正在打印。cgi 中每个递增的值 j 如何发送到 javascript 并在屏幕上打印?如何在C中建立javascript和cgi之间的通信通道?