Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6713807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:25:24+00:00 2026-05-26T08:25:24+00:00

Hello I’m working with ajax that submits to a C++ cgi program. The problem

  • 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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:25:24+00:00Added an answer on May 26, 2026 at 8:25 am

    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!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I am compiling a program with make but I get the error of
Hello I am working with a simulator that uses rcS scripts to boot, this
Hello guys I am having some problems with exact matches while doing a NamedQuery.
Hello , I am currently working on the student association's website. The student's association
Hello! There is a controller and an action which receives one param through GET,
Hello every one. I'm faced with a weird problem. My application has a simple
Hello I just started working with CodeIgniter framework. My current directory structure is Demo(Project
Hello i been trying to get a tokenizer to work using the boost library
Hello I have a Problem with some Buttons I would like to create a
Hello I am building an application that is going to execute a block of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.