当前位置: 代码迷 >> CGI >> ajax传值给CGI,但CGI得不到数据解决方案
  详细解决方案

ajax传值给CGI,但CGI得不到数据解决方案

热度:409   发布时间:2012-04-01 17:23:46.0
ajax传值给CGI,但CGI得不到数据
在function lb(i)中加入alert(x_url)能得到正确的值"bbs_get_grouplist?gn=1&lb=1"
但ajax传回来的值始终是"aaaaaaaaaaa",不知道是什么原因。

main(){
printf("%s","Content-type:text/html;charset=UTF-8;\n\n");
if(getenv("CONTENT-LENGTH")){
int n=atoi(getenv("CONTENT_LENGTH"));
printf("%d",n);
}
else{
printf("%s","aaaaaaaaaaa");
}
}


function lb(i){
var x_lb=document.getElementById("bbs_group1").value;
var x_url="bbs_get_grouplist?gn="+i+"&lb="+x_lb;
if(!req)requestCreate();
if(req){
req.open("GET" , x_url , true);
req.onreadystatechange=function(){if(req.readyState==4 && req.status==200)alert(req.responseText);}
req.send(null);
}
}

------解决方案--------------------
POST时候再检查CONTENT_LENGTH
------解决方案--------------------
GET方法直接读取
QUERY_STRING即可
------解决方案--------------------
给你一个c写的cgi demo,关于环境变量的,应该对一有用.
C/C++ code

// cgi-demo.c: Simple CGI demo program.
// Vers. 2.00 - 04.April 2007
// k.d.walter@t-online.de

// Includes

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

int main (int argc, char *argv[])
{
   char *pEnvPtr;

   printf ("Content-type: text/html\n");
   printf ("\n");
   printf ("<HTML>\n");
   printf ("<HEAD>\n");
   printf ("<TITLE>CGI Environment Variable</TITLE>\n");
   printf ("</HEAD>\n");
   printf ("<BODY>\n");
   
   // SERVER_SOFTWARE

   pEnvPtr= getenv ("SERVER_SOFTWARE");
   printf ("SERVER_SOFTWARE= ");
   if (!pEnvPtr)
      printf ("<NULL-POINTER>\n");
   else
      printf ("%s\n", pEnvPtr);
   printf ("<br>\n");
   
   // SERVER_NAME

   pEnvPtr= getenv ("SERVER_NAME");
   printf ("SERVER_NAME= ");
   if (!pEnvPtr)
      printf ("<NULL-POINTER>\n");
   else
      printf ("%s\n", pEnvPtr);
   printf ("<br>\n");

   // SERVER_PROTOCOL

   pEnvPtr= getenv ("SERVER_PROTOCOL");
   printf ("SERVER_PROTOCOL= ");
   if (!pEnvPtr)
      printf ("<NULL-POINTER>\n");
   else
      printf ("%s\n", pEnvPtr);
   printf ("<br>\n");

   // SERVER_PORT

   pEnvPtr= getenv ("SERVER_PORT");
   printf ("SERVER_PORT= ");
   if (!pEnvPtr)
      printf ("<NULL-POINTER>\n");
   else
      printf ("%s\n", pEnvPtr);
   printf ("<br>\n");

   // REQUEST_URI

   pEnvPtr= getenv ("REQUEST_URI");
   printf ("REQUEST_URI= ");
   if (!pEnvPtr)
      printf ("<NULL-POINTER>\n");
   else
      printf ("%s\n", pEnvPtr);
   printf ("<br>\n");

   // REQUEST_METHOD

   pEnvPtr= getenv ("REQUEST_METHOD");
   printf ("REQUEST_METHOD= ");
   if (!pEnvPtr)
      printf ("<NULL-POINTER>\n");
   else
      printf ("%s\n", pEnvPtr);
   printf ("<br>\n");

   // GATEWAY_INTERFACE

   pEnvPtr= getenv ("GATEWAY_INTERFACE");
   printf ("GATEWAY_INTERFACE= ");
   if (!pEnvPtr)
      printf ("<NULL-POINTER>\n");
   else
      printf ("%s\n", pEnvPtr);
   printf ("<br>\n");

   // HTTP_CONNECTION

   pEnvPtr= getenv ("HTTP_CONNECTION");
   printf ("HTTP_CONNECTION= ");
   if (!pEnvPtr)
      printf ("<NULL-POINTER>\n");
   else
      printf ("%s\n", pEnvPtr);
   printf ("<br>\n");

   // PATH_INFO

   pEnvPtr= getenv ("PATH_INFO");
   printf ("PATH_INFO= ");
   if (!pEnvPtr)
      printf ("<NULL-POINTER>\n");
   else
      printf ("%s\n", pEnvPtr);
   printf ("<br>\n");

   // PATH_TRANSLATED

   pEnvPtr= getenv ("PATH_TRANSLATED");
   printf ("PATH_TRANSLATED= ");
   if (!pEnvPtr)
      printf ("<NULL-POINTER>\n");
   else
      printf ("%s\n", pEnvPtr);
   printf ("<br>\n");

   // REMOTE_HOST

   pEnvPtr= getenv ("REMOTE_HOST");
   printf ("REMOTE_HOST= ");
   if (!pEnvPtr)
      printf ("<NULL-POINTER>\n");
   else
      printf ("%s\n", pEnvPtr);
   printf ("<br>\n");

   // REMOTE_ADDR

   pEnvPtr= getenv ("REMOTE_ADDR");
   printf ("REMOTE_ADDR= ");
   if (!pEnvPtr)
      printf ("<NULL-POINTER>\n");
   else
      printf ("%s\n", pEnvPtr);
   printf ("<br>\n");

   // REMOTE_PORT

   pEnvPtr= getenv ("REMOTE_PORT");
   printf ("REMOTE_PORT= ");
   if (!pEnvPtr)
      printf ("<NULL-POINTER>\n");
   else
      printf ("%s\n", pEnvPtr);
   printf ("<br>\n");
      
   // REMOTE_IDENT

   pEnvPtr= getenv ("REMOTE_IDENT");
   printf ("REMOTE_IDENT= ");
   if (!pEnvPtr)
      printf ("<NULL-POINTER>\n");
   else
      printf ("%s\n", pEnvPtr);
   printf ("<br>\n");

   // SCRIPT_FILENAME

   pEnvPtr= getenv ("SCRIPT_FILENAME");
   printf ("SCRIPT_FILENAME= ");
   if (!pEnvPtr)
      printf ("<NULL-POINTER>\n");
   else
      printf ("%s\n", pEnvPtr);
   printf ("<br>\n");

   // SCRIPT_NAME

   pEnvPtr= getenv ("SCRIPT_NAME");
   printf ("SCRIPT_NAME= ");
   if (!pEnvPtr)
      printf ("<NULL-POINTER>\n");
   else
      printf ("%s\n", pEnvPtr);
   printf ("<br>\n");

   // QUERY_STRING

   pEnvPtr= getenv ("QUERY_STRING");
   printf ("QUERY_STRING= ");
   if (!pEnvPtr)
      printf ("<NULL-POINTER>\n");
   else
      printf ("%s\n", pEnvPtr);
   printf ("<br>\n");

   // CONTENT_TYPE

   pEnvPtr= getenv ("CONTENT_TYPE");
   printf ("CONTENT_TYPE= ");
   if (!pEnvPtr)
      printf ("<NULL-POINTER>\n");
   else
      printf ("%s\n", pEnvPtr);
   printf ("<br>\n");

   // CONTENT_LENGTH

   pEnvPtr= getenv ("CONTENT_LENGTH");
   printf ("CONTENT_LENGTH= ");
   if (!pEnvPtr)
      printf ("<NULL-POINTER>\n");
   else
      printf ("%s\n", pEnvPtr);
   printf ("<br>\n");

   printf ("</BODY>\n");
   printf ("</HTML>\n");
   return (EXIT_SUCCESS);
} 
  相关解决方案