如何在C中建立javascript和cgi之间的通信通道?

时间:2013-10-15 08:24:27

标签: javascript c cgi

如何让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>

在服务器端cgi:C:

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。每个递增的值如何? j在cgi中发送到javascript并在屏幕上打印?如何建立 C中javascript和cgi之间的通信渠道?

2 个答案:

答案 0 :(得分:0)

我用ajax和php尝试过一些东西。这是XHR的客户端:

<html>
    <head>
     <script>
       var response;
     </script>
    </head>

    <body>   
    <p id="demo"></p>
    <input type="button" onclick="sendRequest()" value="String"> <br>

     <script>      
      function sendRequest() {        
        var http = createRequestObject(); 
        http.open("GET", "phptoajax.php?p=");
        http.onreadystatechange = function () { handleResponse(http); };
        http.send(null);        
    }

     function createRequestObject() {
        var http;
        if (navigator.appName == "Microsoft Internet Explorer") {
            http = new ActiveXObject("Microsoft.XMLHTTP");
        }
        else {
            http = new XMLHttpRequest();
        }
        return http;
    }

    function handleResponse(http) {    
        if (http.readyState == 4) {
            response = http.responseText;        
            document.getElementById("demo").innerHTML = response + "<br>";        
        }
    }     
     </script>    
    </body>
    </html>

向php发送内容。 Php然后返回字符串Hello World:

<?php    
  $j = $_GET["p"];
  $j = $j . " Hello World";
  echo   $j ;    
?>

只有echo字符串值$ j,http.responseText才能得到这个 返回值,客户端将“Hello World”打印到浏览器。它有效。

但是当我在cgi中尝试这个时,我会陷入困境。在客户端必须改为 cgi脚本:

http.open("GET", "cgi.exe?p=");

CGI C:

int main() 
{ 

char *data_get;
char *string = "Hello World";

  printf("%s%c%c\n", "Content-Type:text/html;charset=iso-8859-1",13,10);     
  printf(" <html><title>hello</title><body> \n");       

     data_get = getenv("QUERY_STRING"); 

     if( !strcmp(getenv("REQUEST_METHOD"), "GET") ){

         // printf("<br> data_get: %s <br>\n", data_get);             
         printf("%s\n", string); // Sending to browser    
     }
    else if(!strcmp(getenv("REQUEST_METHOD"), "POST")){
        printf("<br> POST METHOD");           
     }          
   printf(" </body></html>");      
  return 0; 
} 

Cgi没有向浏览器发送任何内容。如何使用printf(“%s \ n”,字符串);打印“Hello World”到浏览器就像使用echo $ j一样;来自php?

答案 1 :(得分:0)

问题解决了!

问题是行中cgi脚本的路径:http.open(“GET”,“cgi.exe?p =”);.

必须在本地主机上填写完整路径:

http.open("GET", "http://localhost/cgi-bin/cgi.exe?p=");