Inconsistent performance of AJAX functionality

I've been diving deep into this problem for quite some time now. I have a feeling that someone with the right expertise will be able to pinpoint the issue, but for some reason, it's just not clicking for me.

The basic functionality here revolves around making two API calls. The first call fetches a number when the page loads. Then, upon form submission by the user, a second call is made to increment the total count of submissions in my PHP file.

On most occasions, this setup works flawlessly. Surprisingly, it even seems to function properly on IE7 and IE8 (with some quirks - there appears to be caching involved, leading to updates being reflected only in different browsers).

EDIT: I've tested compatibility with IE7 and IE8 solely through the developer tools provided by IE9; haven't run it on actual installations of those versions myself.

$(document).ready(function() {

  var request = window.ActiveXObject ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest;
      var url = "counter.php?myaction=getcount";
      request.open('GET', url, true);
      request.onreadystatechange = function() {
    if (request.readyState == 4) {
      request.onreadystatechange = doNothing;
      updateCounter(request.responseText);
    }
  };
  request.send(null);

});

    function doNothing() {};

    function addCount() {

  var request2 = window.ActiveXObject ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest;
      var url2 = "counter.php?myaction=addcount";
      request2.open('GET', url2, true);
      request2.onreadystatechange = function() {
    if (request2.readyState == 4) {
      request2.onreadystatechange = doNothing;
      updateCounter(request2.responseText);
              $('#stcPetitionForm').submit();
    }
  };
  request2.send(null);     

    }

    function updateCounter(numSubmissions) {
            $("#myCounter").html("<p>" + numSubmissions + "</p>");
    }

Answer №1

Is it possible that GET requests are being cached? Have you configured no-cache headers on the server side?

Answer №2

This issue has been extensively documented. For helpful solutions, take a look at How to prevent Ajax/javascript result caching in browsers?.

Answer №3

One of the reasons for this issue is that GET requests are being cached.

To prevent caching, you can use the following example:

  • var d    = new Date();
    var url2 = "counter.php?myaction=addcount&time=' + d.getTime();
    // This code should be applied to all URLs, not just url2
    

An illustration of using getTime function:

  • function postTime(){
       var d = new Date();
       console.log(d.getTime());
    }
    
    setInterval(postTime,1000);
    

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

Troubleshooting error handling in Node.js using Express and MongoDB

hey there, I'm new to Node.js and I'm working on creating a basic notes app using express and MongoDB. However, I'm encountering an issue when trying to update a note which displays "Cannot PUT" followed by the note's ID. Below is the ...

Above the search box in jQuery Datatable's search box, there is search text displayed

My datatable looks like this: var exTable1 = $("#exTable").DataTable({ "language": { "search": "Filter", "searchPlaceholder": "search", "loadingRecords": "", }, data: datasrc "dom": '<"top"lf>rt<"botto ...

Is it possible to utilize the returned value of a function within an if statement?

Is there a way to return the result of a function without needing to declare a variable? Can you return the result of a function in a single line? How can you return the result of a function inside an if statement? Is it possible to use a function's ...

Leveraging global attributes beyond Vue components - Vue 3

I have created custom service instances in main.ts app.config.globalProperties.$service1 = new Service1(); app.config.globalProperties.$service2 = new Service2(); While I can use these instances inside Vue components, I also want to be able to utilize the ...

The AJAX request is not executing the xmlhttp.open method for running the script

Currently, I am making adjustments to the tutorial available at this website: http://www.w3schools.com/ajax/ajax_aspphp.asp. My modification involves storing a library of autocomplete names in a database. I have implemented a button that allows users to ...

What is the best approach to testing a function that relies on a reference to Firebase?

I am currently trying to write unit tests for a function within my application that interacts with Firebase, specifically by calling and updating a reference. However, I have encountered an issue while attempting to run the test - upon importing the file c ...

What is the best way to merge two ajax functions into a single function?

I am looking to streamline my ajax functionality by combining two separate functions. One function is used to post a name, while the other is used to upload an image. I would like to create a single function that can handle both tasks of posting a name and ...

Struggling to devise a for loop for a JavaScript-generated list

I'm attempting to loop the content of an H1 element through a list 10 times. I seem to have made a mistake in my code and would appreciate any guidance. var headOne = document.createElement("H1"); headOne.textContent = "Hello World"; document.body. ...

Ensuring a fixed table with vertical scrolling only, no horizontal scrolling, while scrolling in either direction

I'm working with an HTML fixed design that contains a center-aligned table with extensive data scrollable both vertically and horizontally. To address the issue of keeping column headers visible when scrolling vertically, I used jQuery's clone() ...

Using jQuery to make an AJAX call to an API

i have a request $(function(){ $.ajax({ type: 'GET', dataType:"jsonp", url: "http://localhost:8000/api/admin/announces", headers:{ 'Authorization' : 'Bearer eyJhbGci ...

Unexplained Reference Error in Next.js Typescript: Variable Accessed before Initialization

I am currently working on an admin website and encountered the error Block-scoped variable used before its declaration.. I will provide details using images and code. This is my first time seeking help on StackOverflow. Error Message: Block-scoped variab ...

Is there a way to identify the Xpath after clicking a button that adds a row to the JQGrid?

Seeking the Xpath for the Row that was added upon clicking a button. Need to click on the datepicker in the first row and first column to select a date. How can I locate the Xpath for this date picker element? See my code snippet below `<table ...

Google Maps Shifting Focus

I'm currently working on an AngularJS app that involves multiple locations, and the goal is for users to click on a location which then redirects them to the specific spot on Google Maps. However, I've encountered an issue when trying to relocate ...

best practices for data flow between components in React using hooks

I have successfully retrieved data from my database in the Recipes component. Now, I am attempting to pass this data into the RecipeList component. However, when I do so, only the bullet points are showing up in the RecipeList component and for some reas ...

Assigning a value to a variable can prevent the occurrence of an

My recursive code consists of two pieces aiming to print out half of the array recursively until we reach arrays of length 1. Surprisingly, the code without variable assignment runs infinitely, while the code with variable assignment behaves as expected. ...

In Rails, the Ajax-enabled average rating bar refuses to reload

I've been developing a Ruby on Rails Application featuring a Rating System. The rating system is functioning as expected, and I have successfully implemented ajax functionality. Additionally, I am now looking to display the current average rating in a ...

Struggling to iterate through a massive JavaScript object using a for loop

Presented is a sizable javascript object: {"raw": null, "entities": ["CLOUD14", "ABUSE2916-ARIN", "ADMIN2521-ARIN", "NOC11962-ARIN"], "asn_registry": "arin", "network": {"status": null, "handle": "NET-104-16-0-0-1", "name": "CLOUDFLARENET", "links": ["htt ...

The Angular method for retrieving the child's ID when it is clicked

As a newcomer to Angular 1.0 with a background in jQuery, I am facing the following scenario: Let's imagine we have the following HTML structure : <div id="filters"> <div id="filter1">Filter 1</div> <div id="filter2"> ...

"Utilizing Echarts with dynamic data retrieval via AJAX

I attempted to create a chart using chart and ajax data types. However, when I tried to display the results of a call on the xAxis, nothing was showing up. Here is the code for this chart: <div id="main" style="width:600px; height:400px;"></div&g ...

Changing focus to 'DIV' element without JQuery using Javascript and Angular's ng-click event

Instructions for using an Angular directive to set focus on a "DIV" element when clicked <a href="#" class="skipToContent" ng-click="showContent()" title="skip-to-main-content">Skip To Main Content</a> <div class="getFocus" role="button" ...