Ajax 未从 C++ 返回数据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 />
<label>
<input type="checkbox" name="class-int" id="int" />Internet Development
</label>
<label>
<input type="checkbox" name="class-net" id="net" />Networking
</label>
<label>
<input type="checkbox" name="class-gui" id="gui" />GUI
</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;
}
I'm having a problem with some ajax that im working on. The problem is that it is not returning anything in the responseText
field. I would like to return the string "OK"
in response text but i'm not even sure if my cgi program is being called. Here is my html file with the ajax.
<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 />
<label>
<input type="checkbox" name="class-int" id="int" />Internet Development
</label>
<label>
<input type="checkbox" name="class-net" id="net" />Networking
</label>
<label>
<input type="checkbox" name="class-gui" id="gui" />GUI
</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>
The cgi program is written in C++ and it is one directory below in my cgi-bin. Here is my cpp code for the cgi program
#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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论