Avoid ajax caching in jQuery Mobile

I have come across questions related to preventing caching in jQuery, but I am specifically looking for a solution for jQueryMobile and PhoneGap using JSONP. My aim is to make ajax calls that dynamically populate a listview, querying the web service every time the page initializes (pageinit). However, on my Android device, each new query results in data being added to the listview without deleting the previous data, leading to duplicates. I suspect this issue is related to caching. To address it, I have included the following code snippet:

$(document).bind('pageinit', function() {
    $.ajaxSetup ({
        cache: false
     });
});

While this prevents caching in Chrome and Safari browsers, it does not seem to work on Android devices. How can I prevent caching on Android devices?

PS. Although similar, this question is not the same: How to prevent caching from jQuery Ajax?

Answer №1

To prevent caching, generate a random value using the random generator function and pass it as a parameter in each AJAX request you make.

var randomNumber = Math.floor((Math.random()*1000)+1);
$.ajax({
  url: 'ajax/test.html', data: {rand: randomNumber}, 
  success: function(data) {
    $('.result').html(data);
    alert('Content loaded successfully.');
  }
});

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

Unable to programatically alter toggle button

Having trouble setting a ToggleButton to off programmatically - I've tried various solutions but can't seem to get it to work. Here is the relevant code snippet: while (toggleButton4_.isChecked()){ seekBar_.setProgress(0); toggleButton2_.setSe ...

management in the same row or in the following row fashion

Looking to achieve a specific behavior for a label and control: If the screen width is less than X, the label and control should be on different rows with the control expanding to full width. If the width is equal to or greater than X, the label and cont ...

Uploading files asynchronously in node.js with Ajax

Looking to implement file upload with ajax using the uploader found at In node.js, the following code works for normal file uploads without ajax: console.log("request " + sys.inspect(req.headers)); req.on('data', function(chunk) { ...

I need help crafting a regular expression that specifically prohibits the use of semicolons, colons, single quotes, and

I am attempting to create a regular expression that does not allow semi-colons, colons, single quotes, and double quotes. var address=/^[^\u0022\u0027\u003A\u003B]{1,50}$/ address.test(value); This is my code. This code will only ru ...

Steps to Launch a Popup Window within an iFrame

I am attempting to present a popup window from an iframe using two jsp files: main.jsp <form name="searchform"> Search for :<input type="text" id="search"/> </form> <iframe name="popup" src="popup.jsp" height=" ...

Unable to view HTML without an internet connection

I have encountered an issue where my files work fine when uploaded to an online server, but do not work when accessed locally offline. An error message about a cross-origin issue appears. How can I solve this problem? Error message: Security Error: Conte ...

What is the best way to create a dynamic-width text input with ellipsis and label in Angular?

I am looking to incorporate a <input type="text"> element in my Angular application that will automatically display an ellipsis if the user-provided value is too lengthy to fit within the UI, unless being edited. This text input will have a dynamic ...

What is the best method for placing a button at the bottom of a RecyclerView?

Is there a way to display a button at the bottom of a RecyclerView, similar to how it was done with ListView using the addFooterView() method? ...

Can one verify if an Angular application currently has active app modules running?

I am developing a unique plugin for Angular that is designed to automatically initialize an Angular app module if none are found. However, if there is already a running or declared ng-app, my plugin will utilize that existing module instead. Here is an i ...

Converting Vanilla JavaScript into React Framework

Recently, I coded a basic JavaScript function to move a div around on mouseover by utilizing the getBoundingClientRect() method. iconsArray.forEach((icon) => { icon.addEventListener('mouseover', function() { const leftOfIcon = this.ge ...

Error: The specified file or directory does not exist at location 'D:E-commerceJsfrontend ode_modules.axios.DELETE'

As I work on my e-commerce project using vanilla JavaScript and webpack with npm, I keep encountering an issue while trying to install axios. npm ERR! enoent ENOENT: no such file or directory, rename 'D:\E-commerceJs\frontend\node_mod ...

Steps to resolve the "cannot get product" error on Heroku after submitting a value

view the image description hereI have been working on a MERN full stack project and everything was functioning correctly in my local environment. However, upon deploying it to Heroku, I encountered an error which is displayed below. All other APIs are fu ...

Link the <select> element with ng-options and ng-model, ensuring that the model contains a composite key

I am facing a challenge with my Angular service that retrieves a list of objects with a composite key comprising two parts. I am struggling to write the bindings in a way that properly re-binds existing data. Below is my current attempt: angular.module( ...

Getting errors while working with Expo code in React Native is a common occurrence

I'm attempting to implement drawer navigation in a Snack using Expo's sample code directly from the documentation on Drawer Navigation in React Native You can find the Snack code at this link: Code Sample for troubleshooting Unfortunately, the ...

creation of objects using constructors in Node.js

As I delve into the world of Node.js, a burning question has arisen - how can I make a function accept multiple strings in the form of an array? Picture this scenario: export default (config: Config) => { return { target: 'https://google.com ...

React Bootstrap encountered rendering issues

Recently, I decided to delve into the world of React Bootstrap and started my learning journey. To get started, I followed some tutorials on <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <meta name="vi ...

Is it possible to switch the audio source randomly when hovering?

(I'm still learning the ropes of JS and jQuery, so please bear with me!) My goal is to dynamically change the source of an audio element when hovering over other elements. I managed to achieve this using inline JS through a button (though I can' ...

Removing an item using javascript

I have implemented a compact panel using the code below, allowing users to toggle the content display by clicking on it: $('.body_panel').find('li').click(function() { if ($(this).has("ul")) { $(this).children(& ...

How can we incorporate Django template tags into our jQuery/JavaScript code?

Is it possible to incorporate Django's template tags within JavaScript code? For example, utilizing {% form.as_p %} in jQuery to dynamically inject forms onto the webpage. ...

Experiencing difficulties when attempting to access a relative URL in a Javascript xmlhttp object

Recently, I took on the task of managing an HTML/PHP/Javascript application that lacked proper directory structure; everything was crammed into one folder. My goal was to organize and modularize the files, so I decided to move them around. (*.inc files ...