C+CGI+AJAX+Win64+Apache上的配置

来源:互联网 发布:apk制作软件安卓版 编辑:程序博客网 时间:2024/05/29 09:49

想写个前台页面发送ajax请求到后台用c语言编写的cgi程序,并成功返回结果。源码都很简单,都有,结果在调试配置时遇到了一个又一个问题。特记录之。

过程参考:http://blog.sina.com.cn/s/blog_5dd73f550100sc6t.html

1、先贴代码:ajaxtest1.htm

<html>
<head>
<script type="text/javascript">

function FuncA(str)
{
var xmlhttp;
if(str.length==0)
 
  document.getElementById("txtIDA").innerHTML="";
  return;
  }
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("txtIDA").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","/ajaxtest1cgi/ajaxtest1.cgi?txtIDA="+str,true);
xmlhttp.send();
}

function FuncB(str)
{
var xmlhttp;
if(str.length==0)
 
  document.getElementById("txtIDB").innerHTML="";
  return;
  }
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("txtIDB").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","/ajaxtest1cgi/ajaxtest1.cgi?txtIDB="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>

<h3>send data to background CGI through ajax:</h3>
<h3>background send data to front:</h3>
<form action=""> 
name:<input type="text" id="txt1" onkeyup="FuncA(this.value)" />
age:<input type="text" id="txt2" onkeyup="FuncB(this.value)" />
</form>
<p>suggest:<span id="txtIDA"></span></p> 
<p>instruction:<span id="txtIDB"></span></p> 

</body>
</html>


cgi_main.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) 
{
char *lenstr;

if(lenstr=getenv("QUERY_STRING"))
{
}
else
{
}

    printf("Content-type: text/html;Charset=utf-8\n\n");

if(strstr(lenstr,"txtIDA")!=NULL)
{
   printf("xianxiangdong\n\n");
}
if(strstr(lenstr,"txtIDB")!=NULL)
{
   printf("i am 27 years old\n\n");
}

return 0; 

2、用dev-c++编译生成cgi执行文件。

之前我用ubuntu上的gcc编译生成cgi,结果访问后显示乱码,或者显示下载该cgi文件。懵逼了,显示下载是因为我的apache服务器没有配置对cgi文件的执行的支持,需要在apache配置文件中进行配置,详见http://blog.csdn.net/naturebe/article/details/7443662。主要就三步:LoadModule cgi_module modules/mod_cgi.so,Options Indexes FollowSymLinks MultiViews ExecCGI,AddHandler cgi-script .cgi .pl .py .sh。网上有说我apache版本高了,要回到旧版本的,胡说八道。

显示乱码是因为我是在ubuntu中用gcc编译的,然后想放在windows10上跑,当然跑不过啦。在apache中的日志文件中看错误是这样的:gcc: Internal error: Aborted (program collect2)

[Wed May 24 14:51:10.505032 2017] [win32:error] [pid 1840:tid 1136] [client 127.0.0.1:63408] AH02102: E:/Program Files/Apache24/htdocs/ajaxtest1.cgi is not executable; ensure interpreted scripts have "#!" or "'!" first line
[Wed May 24 14:51:10.505032 2017] [cgi:error] [pid 1840:tid 1136] (9)Bad file descriptor: [client 127.0.0.1:63408] AH01222: don't know how to spawn child process: E:/Program Files/Apache24/htdocs/ajaxtest1.cgi。所以需要在windows10上用gcc编译生成cgi。然后我下载安装了dev-c++,一定要是Dev-Cpp 5.3.0.3 TDM-GCC x64版本的,4.9.9.2版本的不支持64位。链接:https://down.bccn.net/4195.html。配置环境变量,用gcc命令编译,参见:http://bbs.chinaunix.net/thread-768151-1-1.html。

3、文件目录:


4、调试结果:



原创粉丝点击