Ajax readState 始终等于 1

发布于 2024-12-11 09:03:53 字数 1635 浏览 0 评论 0原文

你好,我正在使用 ajax 提交到 C++ cgi 程序。我遇到的问题是readyState 始终为1。我不明白我做错了什么。

    var asyncRequest; // XMLHttpRequest object

    try
        {
            asyncRequest = new XMLHttpRequest();

            // Register event handler
            asyncRequest.onreadystatechange = StateChange; 

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

            //Data to be sent to cgi program
            postData="star=1&movie=test";


            // 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()
{

    // Make sure request has completed with 200 OK status

    //alert(asyncRequest.status)
    if (asyncRequest.readyState == 4 && asyncRequest.status == 200)
    {
        alert("Hello");
    }

}

这是cgi程序

#include "cgi.h"
#include <fstream>
    int main()
{
    cout << "Content-type: text/html\n\n";

    ParseInputParameters();

    ofstream fout;

    if (fout.fail())
    {
        cout << "CGI Error - Couldn't open file for appending for appending.";
        return 0;
    }


    //message to be sent back in the response text
        cout << "OK";

    fout.close();

    return 0;
}

Hello I'm working with ajax that submits to a C++ cgi program. The problem that I am having is the readyState is always 1. I don't get what I am doing wrong.

    var asyncRequest; // XMLHttpRequest object

    try
        {
            asyncRequest = new XMLHttpRequest();

            // Register event handler
            asyncRequest.onreadystatechange = StateChange; 

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

            //Data to be sent to cgi program
            postData="star=1&movie=test";


            // 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()
{

    // Make sure request has completed with 200 OK status

    //alert(asyncRequest.status)
    if (asyncRequest.readyState == 4 && asyncRequest.status == 200)
    {
        alert("Hello");
    }

}

Here is the cgi program

#include "cgi.h"
#include <fstream>
    int main()
{
    cout << "Content-type: text/html\n\n";

    ParseInputParameters();

    ofstream fout;

    if (fout.fail())
    {
        cout << "CGI Error - Couldn't open file for appending for appending.";
        return 0;
    }


    //message to be sent back in the response text
        cout << "OK";

    fout.close();

    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

记忆で 2024-12-18 09:03:53

将 SateChange 函数移至生成 XHR 的函数主体。进行此修改后,StateChange 内的 asyncRequest 将等于相关的 asyncRequest 对象。

function ajaxUpdate(){
    ....
    var asyncRequest; // XMLHttpRequest object
    try {
        asyncRequest = new XMLHttpRequest();
        ....
        asyncRequest.send(postData);
     }
     catch (exception){
        alert("Request failed: " + exception.message);
     }

    //} <---Removed curly bracket
    function StateChange(){

        // Make sure request has completed with 200 OK status

        //alert(asyncRequest.status)
        if (asyncRequest.readyState == 4 && asyncRequest.status == 200){
            alert("Hello");
        }
    }

} //<--Added curly bracket!

Move the SateChange function to the body of the function where the XHR is being made. After this modification, the asyncRequest inside StateChange will equal the relevant asyncRequest object.

function ajaxUpdate(){
    ....
    var asyncRequest; // XMLHttpRequest object
    try {
        asyncRequest = new XMLHttpRequest();
        ....
        asyncRequest.send(postData);
     }
     catch (exception){
        alert("Request failed: " + exception.message);
     }

    //} <---Removed curly bracket
    function StateChange(){

        // Make sure request has completed with 200 OK status

        //alert(asyncRequest.status)
        if (asyncRequest.readyState == 4 && asyncRequest.status == 200){
            alert("Hello");
        }
    }

} //<--Added curly bracket!
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文