#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    9
    Rep Power
    0

    How Cgi return the response from an ajax call


    This is client ajax call:
    Code:
    <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", "cgi.exe?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>
    Cgi server in C:
    Code:
    int main() 
    { 
    
    char *string = "Hello World";
    
     printf("Content-Type: text/html");       
    
         if( !strcmp(getenv("REQUEST_METHOD"), "GET") ){
             printf("%s", string); // Sending to browser    
         }
      return 0; 
    }
    Doesnt write anything to:
    document.getElementById("demo").innerHTML = response;

    What im doing wrong, please help!
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    9
    Rep Power
    0

    Problem solved


    Problem was path to cgi script in line : http.open("GET", "cgi.exe?p=");.
    Have to put full path on local host:
    http.open("GET", "http://localhost/cgi-bin/cgi.exe?p=");

    Problem solved!

IMN logo majestic logo threadwatch logo seochat tools logo