XMLHttpRequest 和 C CGI...谁只发回一个字
XMLHttpRequest and C CGI...who sends back just one word
我正在用一个简单的表单进行测试,脚本会获取文本区域的值并将其发送到 "sample.cgi",现在,"sample.cgi" 应该只发送回准确的文本我发给他...但是,我只收到第一个字。
我真的找不到问题。
代码如下:
<!DOCTYPE html>
<html>
<head>
<script>
function mytest() {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
//If everything is ok show the output whit an alert
alert("This is the server answer: " + xhr.responseText);
}
};
var myText = document.getElementById("message").value;
//Request to "sample.cgi"
xhr.open("POST", "sample.cgi", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
//Sending the data
xhr.send(myText);
}
</script>
</head>
<body>
<h2>MY TEST: </h2>
<form>
<textarea id="message">
</textarea>
<button type="button" onclick="mytest()">Request data</button>
</form>
</body>
</html>
sample.gci代码:
#include <stdio.h>
int main(void)
{
printf("Content-Type: text/plain;\n\n");
char myText[1000];
//the data is sent by "POST" method, then we find it in stdin
fscanf(stdin,"%s", myText);
printf("%s",myText);
return 0;
}
提前致谢
非常感谢我使用 fgets 解决了!没想到"mytext"是没有格式化的数据。
我想再问一个小问题,为什么我改了我的C代码没有得到任何结果:
int main(void)
{
printf("Content-Type: text/plain;\n\n");
FILE * pFile;
pFile = fopen ("sample.txt","w");
char myText[1000];
fgets(myText,1000, stdin);
fprintf(pFile, "%s\n",myText);
printf("We stored this text: %s",myText);
fclose(pFile);
return 0;
}
显然 "xhr.onreadystatechange" 甚至没有收到发送警报的正确条件。
非常感谢您的帮助!
我正在用一个简单的表单进行测试,脚本会获取文本区域的值并将其发送到 "sample.cgi",现在,"sample.cgi" 应该只发送回准确的文本我发给他...但是,我只收到第一个字。 我真的找不到问题。
代码如下:
<!DOCTYPE html>
<html>
<head>
<script>
function mytest() {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
//If everything is ok show the output whit an alert
alert("This is the server answer: " + xhr.responseText);
}
};
var myText = document.getElementById("message").value;
//Request to "sample.cgi"
xhr.open("POST", "sample.cgi", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
//Sending the data
xhr.send(myText);
}
</script>
</head>
<body>
<h2>MY TEST: </h2>
<form>
<textarea id="message">
</textarea>
<button type="button" onclick="mytest()">Request data</button>
</form>
</body>
</html>
sample.gci代码:
#include <stdio.h>
int main(void)
{
printf("Content-Type: text/plain;\n\n");
char myText[1000];
//the data is sent by "POST" method, then we find it in stdin
fscanf(stdin,"%s", myText);
printf("%s",myText);
return 0;
}
提前致谢
非常感谢我使用 fgets 解决了!没想到"mytext"是没有格式化的数据。
我想再问一个小问题,为什么我改了我的C代码没有得到任何结果:
int main(void)
{
printf("Content-Type: text/plain;\n\n");
FILE * pFile;
pFile = fopen ("sample.txt","w");
char myText[1000];
fgets(myText,1000, stdin);
fprintf(pFile, "%s\n",myText);
printf("We stored this text: %s",myText);
fclose(pFile);
return 0;
}
显然 "xhr.onreadystatechange" 甚至没有收到发送警报的正确条件。
非常感谢您的帮助!