Receiving feedback from an Ajax request

When attempting to retrieve the responseText from an AJAX call created in plain JavaScript, there seems to be an issue where Firebug can detect the request but cannot obtain a reference to the responseText.

Below is the code for the function:

function getAjaxResponse(){    
    var ajaxObj = getAjaxObj();
    ajaxObj.open('get', 'responsePage.php', true);
    ajaxObj.onReadyStateChanged = function(){
        if(ajaxObj.readyState == 4
            && ajaxObj.status == 200){
                //no functions are getting executed here                
                //this does not show up in console
                console.log(ajaxObj.responseText);
                //neither does this
                console.log(2);
        }
    };
    ajaxObj.send(null);

   //this gets displayed in the console
   console.log(1);
}

Function for the AJAX object:

function getAjaxObj(){
    var req;  
    if(window.XMLHttpRequest){
        try{
            req = new XMLHttpRequest();                                                                 
        } catch(e){
            req = false;
        } finally {
            return req;
        }
    } else {
        if(window.ActiveXObject){
            try{
                req = new ActiveXObject("Msxml2.XMLHTTP");
            } catch(e){
                try{
                    req = new ActiveXObject("Msxml.XMLHTTP");
                } catch(e){
                    req = false;
                } finally {
                    return req;
            }
            }
        }
    }
}

Also attached is the view from Firebug:

How can one access the response from the AJAX call?

Answer №1

onReadyStateChanged should actually be written as onreadystatechange. Remember, JavaScript is case-sensitive.

Answer №2

ajaxObj.onReadyStateChanged: For consistency, it is recommended to use all lowercase letters and remove the trailing 'd' in onreadystatechange.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Service for Posting in Angular

I am looking to enhance my HTTP POST request by using a service that can access data from my PHP API. One challenge I am facing is figuring out how to incorporate user input data into the services' functionality. Take a look at the following code snip ...

Is it necessary to convert an HTMLCollection or a Nodelist into an Array in order to create an array of nodes?

Here we go again with the beginner guy. I'm working on this exercise from a book called "Eloquent JavaScript". The goal is to create a function similar to "getElementByTagName". However, the first function below is not returning anything and the secon ...

Issue with express-http-proxy where relative URL paths are not functioning as expected

My server is hosting an app along with a few simple web microservices. I want to access these services over the internet without having to open individual ports for each one. To achieve this, I decided to set up a reverse proxy on the server using express- ...

Combining CodeIgniter4 with Vue.js and Webpack's devServer to handle CORS issues

Exploring Vue & CodeIgniter 4, starting from https://github.com/flavea/ci4-vue. No matter what I try, I encounter a persistent CORS error in dev mode: Access to XMLHttpRequest at 'http://example.com/public/api/book/get' from origin &apo ...

Is it possible to continuously loop and gradually fade the same image URL using JavaScript?

I have a dynamic image stored on my web server at example.com/webcam.jpg that is continuously updated by the server. My goal is to display this image on a static HTML page with a looping effect that smoothly transitions from the previous image to the upda ...

Should I convert to an image or utilize the canvas?

I'm debating whether it's more efficient to convert a canvas drawing into an image before inserting it into the DOM, or if it's better to simply add the canvas itself. My method involves utilizing canvas to generate the image. ...

When using a master page in ASP.Net webforms, the autocomplete feature may not function correctly

I'm encountering an issue with a script on my website. I have a web form called CoursesPage.aspx that utilizes a master page. I have implemented autocomplete functionality using jQuery on a textbox to display course names fetched from a database. The ...

Tips for safeguarding AJAX or javascript-based web applications

Utilizing AJAX, this function retrieves information about an image in the database with the ID of 219 when a button is clicked. Any visitor to this webpage has the ability to alter the JavaScript code by inspecting the source code. By modifying the code a ...

The method of implementing an index signature within TypeScript

I'm currently tackling the challenge of using reduce in Typescript to calculate the total count of incoming messages. My struggle lies in understanding how to incorporate an index signature into my code. The error message that keeps popping up states: ...

Issue with jQuery ajax when sending data via POST method: incorrect data is being transmitted

Here is the code snippet that sends data when a button is clicked: $.ajax({ type: "POST", url: "<?php echo base_url(); ?>coformation/appoint/", data: { "director":director.attr('checked'), "shareholder": ...

Show a plethora of images using the express framework

I have two closely related questions that I am hoping to ask together. Is there a way for express (such as nodejs express) to handle all requests in the same manner, similar to how http treats requests with code like this: pathname = url.parse(request.url ...

Tips for customizing the AjaxComplete function for individual ajax calls

I need help figuring out how to display various loading symbols depending on the ajax call on my website. Currently, I only have a default loading symbol that appears in a fixed window at the center of the screen. The issue arises because I have multiple ...

Using jQuery to locate and delete multiple attributes from div elements

My goal is to locate all div elements with the class name "comment-like" that also have data-id attributes set to 118603,1234,1234,118601,118597. If a div contains any of these data values, then I want to remove that data attribute. I've attempted th ...

Sinon - observing the constructor function

While I've come across a few related inquiries, none seem to address what I am specifically looking to achieve. My goal is to monitor a constructor method in such a way that when an object created with the constructor calls this method from a differe ...

Difficulties with toggling jQuery menus

I am trying to create a sidebar that will appear with a full screen fade over the entire page when I click on the 'navigate' text. Currently, the sidebar appears and my header text moves to the left. However, I am struggling to make it responsiv ...

ajax function only successfully executes on the initial attempt

In the controller, I have a method that allows users to check the "POE Status." Once this method is called, it returns a string that is displayed in a div tag on the view. Additionally, there are two buttons provided - one for enabling POE and the other fo ...

The Ajax request is ineffective on mobile devices and fails to function as intended

I am brand new to using ajax and jquery! My code works perfectly on desktop PCs, but on mobile devices the ajax request is not functioning. Here is the code snippet: <script type="text/javascript"> var startTime = new Date(); //Start the cl ...

The jQuery Mobile framework fails to initialize when loading data from an AJAX request in JSON format

I'm currently attempting to incorporate the jQuery Mobile 'styles' (specifically for buttons) into my project. Below is the HTML code snippet I am using (with Ajax): <!-- Using local jQuery + Mobile files --> <link rel="stylesheet ...

Error encountered: Scrollanimation IOS syntax error- Unexpected token '=.' an open parenthesis '(' was expected before a method's parameter list

Encountering an issue with the scroll animation on older IOS devices (2019 and older) - I receive the following error message: SyntaxError: Unexpected token '='. Expected an opening '(' before a method's parameter list. class Event ...

An error is occurring in asp.net where the ajax call is returning an undefined object

Here is the C# getter method that retrieves meanings of a word input by a user through a query. The meanings are then filled in a data table and added to a list of strings using a loop. Finally, the list is converted into a JSON string and returned: publ ...