Pass data to CGI script and back with jQuery.ajax

The HTML code: (index.php)

点击(此处)折叠或打开

  1. <html>
  2.   <head>
  3.     <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
  4.     <script type="text/javascript" src="20120721.js"></script>
  5.   </head>
  6.   <body>
  7.     <div id="loginContent" class="container">
  8.         <div id="loginResult" style="display:none;">
  9.         </div>
  10.         <form id="loginForm" name="loginForm" method="post" action="">
  11.         <fieldset>
  12.             <legend>Enter information</legend>
  13.             <p>
  14.             <label for="firstname">First name</label>
  15.             <br />
  16.             <input type="text" id="firstname" name="firstname" class="text" size="20" />
  17.             </p>
  18.             <p>
  19.             <label for="name">Name</label>
  20.             <br />
  21.             <input type="test" id="name" name="name" class="text" size="20" />
  22.             </p>
  23.             <p>
  24.             <button type="submit" class="button positive">
  25.              Login
  26.             </button>
  27.             </p>
  28.         </fieldset>
  29.         </form>
  30.     </div>
  31.   </body>
  32. </html>



The JavaScript Code: (20120721.js)

点击(此处)折叠或打开

  1. $(document).ready(function(){
  2.   $("form#loginForm").submit(function() { // loginForm is submitted


  3. var firstname = $('#firstname').attr('value'); // get first name
  4. var name = $('#name').attr('value'); // get name

  5. if (firstname && name) { // values are not empty
  6.     $.ajax({
  7.         type: "GET",
  8.         url: "/cgi-bin/test.fcg", // URL of the Perl script

  9.         // send firstname and name as parameters to the Perl script
  10.         data: "firstname=" + firstname + "&name=" + name,

  11.         // script call was *not* successful
  12.         error: function() {
  13.             alert("script call was not successful");
  14.         },

  15.         // script call was successful
  16.         // perl_data should contain the string returned by the Perl script
  17.         success: function(perl_data){
  18.             alert("Your name is: " + perl_data)
  19.         }
  20.     });
  21. }

  22. else {
  23.   $('div#loginResult').text("Enter your name");
  24.   $('div#loginResult').addClass("error");
  25. }

  26. $('div#loginResult').fadeIn();
  27. return false;
  28.   });
  29. });



The C code: (test.c)

点击(此处)折叠或打开

  1. #include "fcgi_stdio.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stddef.h>
  5. #include <time.h>
  6. int main(void)
  7. {
  8.     while (FCGI_Accept() >= 0) {
  9.         int a=100;
  10.         int b=200;
  11.         printf("Content type: text/html\n\n");
  12.         //printf("%s",a);
  13.         printf("%s",getenv("REQUEST_METHOD"));
  14.         printf("|");                            //输出“|”作为分隔ab值
  15.         printf("%s",getenv("QUERY_STRING"));    //This seems to be working fine with form "GET" method but not with "POST" method. POST data is appended to the request header, after a double newline. In a CGI-BIN environment, you read it from STDIN. http://stackoverflow.com/questions/5451913/how-to-retrieve-form-post-data-via-cgi-bin-program-written-in-c

  16.     }
  17. }

[root@localhost cbpm]# pwd
/var/www/html/cbpm
[root@localhost cbpm]# ls
20120721.js  index.php

[root@localhost cgi-bin]# pwd
/var/www/cgi-bin
[root@localhost cgi-bin]# gcc test.c -o test.fcg -lfcgi

[root@localhost cgi-bin]# uname -a
Linux localhost.localdomain 4.2.1-1.el7.elrepo.x86_64 #1 SMP Mon Sep 21 20:01:19 EDT 2015 x86_64 x86_64 x86_64 GNU/Linux

[root@localhost cgi-bin]# cat /etc/redhat-release
CentOS Linux release 7.1.1503 (Core)

[root@localhost cgi-bin]# systemctl restart httpd.service










<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(98) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
posted @ 2016-02-01 00:00  张同光  阅读(110)  评论(0编辑  收藏  举报