Ajax不从C ++ cgi脚本返回数据

时间:2011-10-20 02:07:34

标签: javascript ajax cgi

我遇到了一些我正在努力的ajax的问题​​。问题是它没有在responseText字段中返回任何内容。我想在响应文本中返回字符串"OK",但我甚至不确定我的cgi程序是否被调用。这是我的带有ajax的html文件。

<html>
  <head>
    <title>Survey</title>



<script>

var asyncRequest; // XMLHttpRequest object

function PostData()
{
    // Attempt to create the XMLHttpRequest and make the request
        try
        {
            asyncRequest = new XMLHttpRequest();

            // Register event handler
            asyncRequest.onreadystatechange = StateChange; 

        // Prepare to post data to URL asynchronously
         asyncRequest.open("POST", "cgi-bin/survey_save.cgi", true); 

        postData = ""; 
        if (document.getElementById("int").checked == true) 
              postData += "class-int=on&"; 
           if (document.getElementById("net").checked == true) 
              postData += "class-net=on&"; 
            if (document.getElementById("gui").checked == true)           
          postData += "class-gui=on&"; 
            if (document.getElementById("oop").checked == true) 
              postData += "class-oop=on&"; 
        if (document.getElementById("gender-male").checked == true) 
            postData += "gender=M"; 
            if (document.getElementById("gender-female").checked == true) 
             postData += "gender=F";


            // Set the appropriate HTTP request headers
        asyncRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            asyncRequest.setRequestHeader("Content-length", postData.length);

            // Make request
            asyncRequest.send(postData);
         }
         catch (exception)
         {
            alert("Request failed: " + exception.message);
         }
}

function StateChange()
{


    //alert(asyncRequest.readyState)
    //alert(asyncRequest.status+"   "+asyncRequest.readyState)      
    if (asyncRequest.readyState == 4 && asyncRequest.status == 200) 
    { 

        alert(asyncRequest.responseText)

            if (asyncRequest.responseText == "OK") 
        {
                 document.getElementById("message").innerHTML = "Thank you!";
             alert(asyncRequest.responseText);
        } 
            else 
                document.getElementById("message").innerHTML =   
                     asyncRequest.responseText; 
    }


}


</script>
</head>
<body>
     <h3>Please Enter the following Information: </h3>

  <form>
    <input type ="hidden" name="input-source" value="survey2.html" />
     <p>
      Name: <input type="text" name="name" size="30" />
      </p><p>
      Classification:
      <select name="class">
        <option>Freshman</option>
    <option selected="selected">Sophomore</option>
    <option>Junior</option>
    <option>Senior</option>
     </select>
    </p><p>
      Gender:
        <input type="radio" name="gender" value="M" checked="checked" id="gender-male" />Male
    <input type="radio" name="gender" value="F"  id="gender-female"/>Female
    </p><p>
      Email address: <input type="text" name="email" size="30" />
          </p><p>
    Password: <input type="password" name="pword" />
    </p><p>
    What are your favorite computer classes?
    <br /> &nbsp;
    <label>
    <input type="checkbox" name="class-int" id="int" />Internet Development &nbsp;
    </label>
    <label>
    <input type="checkbox" name="class-net" id="net" />Networking &nbsp;
    </label>
    <label>
    <input type="checkbox" name="class-gui" id="gui" />GUI &nbsp;
    </label>
    <label>
    <input type="checkbox" name="class-oop" id="oop" />OOP
    </label>
    </p><p>
    Are you graduating this spring?
    <label>
    <input type="radio" name="grad" value="Yes" />Yes
    </label>
    <label>
    <input type="radio" name="grad" value="Yes" />No
    </label>
    </p><p>
     Other information we should know? <br />
     <textarea name ="comments" rows="3" cols="50"></textarea>
      <input type="button" value="Submit Survey" onclick="PostData()"/>
      <input type="reset" value="Clear Form" />
   </p>
   </form>
   <div id ="message" />
  </body>
  </html>

cgi程序是用C ++编写的,它是我的cgi-bin下面的一个目录。这是我的cgi程序的cpp代码

#include<fstream>
#include "cgi.h"

int main()
{

  string name = GetValue("name");
  string gender = GetValue("gender");
  string net = GetValue("class-net");
  string webdev = GetValue("class-int");
  string gui =   GetValue("class-gui");
  string oop = GetValue("class-oop");
  ofstream fout;
  ParseInputParameters();
  cout <<"Content-type:text/html\n\n";

  fout.open("classes.dat", ios::app);

  if(fout.fail())
  {
    cout << "Cannot open up the data file";
  }

  else
  {
    cout <<"OK";
    if( net == "on" )
    {
        fout << "class-net "<<gender<<endl;
        cout << "OK";
    }
    if( webdev == "on" )
    {
            fout << "class-int "<<gender<<endl;
        cout << "OK";
    }
    if( gui == "on" )
    {     fout << "class-gui "<<gender<<endl;
          cout << "OK";
    }
     if( oop == "on" )
    {
         fout << "class-oop "<<gender<<endl;
         cout << "OK";   
    }
  }

  fout.close();
   return 0;
}

0 个答案:

没有答案