Modify the data being sent to the AJAX call within the complete function

My task involves modifying the data sent to an ajax call within the complete function.

For example, I initialize var total = 1; after the ajax call has begun execution.

function test(total) {
    total = total;
    alert("total : " + total);
}
$('#call_analysis_basic_table').DataTable( {
    "processing": true,
    "serverSide": true,
    "iDisplayLength": 100,
    "ajax":{
        type: "POST",
        url :"http://localhost:8050/phpservice/index.php", // json datasource
        data: {
            "rec" : total,
            "phone" : "d123",
            "from_date" : "14-05-2016" ,
            "to_date" : "20-09-2017"
        },
        error: function(){  
            alert("error");
        },
        complete: function(data){  
            total = data.responseJSON.recordsTotal;
            test(total);
        }
    }
} );

Initially, the value of the parameter rec is 1. However, upon updating the value of total in the complete function (now set at 20), when I navigate to page 2 of the table, the ajax call reverts back to sending a rec value of 1 instead of 20. Why does this happen? Even though I modify the parameter's value in the complete function, it reverts back to the original value during subsequent ajax calls.

Answer №1

When sending data to the server, the data value has the flexibility of being a function that is called to retrieve the necessary data:

data: function(d) {
    return $.extend({}, d, {
        "rec" : total,
        "phone" : "d123",
        "from_date" : "14-05-2016" ,
        "to_date" : "20-09-2017"
    };
},

By utilizing this approach, the updated value of total will be used.

For further details, please refer to the ajax.data documentation.

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

Steps for inserting an additional header in an Angular table

https://i.stack.imgur.com/6JI4p.png I am looking to insert an additional column above the existing ones with a colspan of 4, and it needs to remain fixed like a header column. Here is the code snippet: <div class="example-container mat-elevation-z8"> ...

What is the best way to properly pass parameters?

const root = { user: (id) => { console.log("returning object " + JSON.stringify(id.id) + " " + JSON.stringify(storage.select("users", id.id))) return storage.select("users", id.id) } } Struggling to correctly pass the parameter ...

Using jQuery, you can submit a form easily

In my work with Django, I am developing a quiz web application that requires a form submission after answering each question. The questions are displayed correctly, but I am facing an issue when trying to submit the form with the answers. The answers to t ...

What is the best method for extracting an attribute value from an HTML <a> element?

I've been attempting to scrape the specified website, but I'm having trouble retrieving the value of the 'data-link' attribute. Is there anyone who can assist me with this issue? //first try (resulting in an error) const puppeteer = ...

Using Python with AJAX for Dynamic HTTP Responses

I am working on responding to an ajax request using a Python script. The ajax request sends a JSON object in the following manner: $.ajax({ url : "cgi-bin/mycgi.py", type : "POST", data : JSON.stringify(MyJSONObject), success : functio ...

Response Code 500 - Server Encountered an Internal Error

Hello, I am currently working on an ajaxForm using comments. I encountered a 500 Internal Server Error and I'm not sure why. Here is the code snippet causing the issue: <script> $(document).ready(function(){ $("#tbl_comments").hide(); ...

Vuetify Error - "Attempting to access 'extend' property of an undefined object"

Upon installing Vuetify, I encountered an error in the console. My webpack version is v3.6 Uncaught TypeError: Cannot read property 'extend' of undefined at Module../src/mixins/themeable/index.ts (index.ts:21) at __webpack_require__ (boo ...

Effortless File Upload with Django Using AJAX and JQuery

I've been struggling to upload a basic text file for quite some time now, but I just can't seem to make it work. Every time I try, I keep getting error messages stating that the "file_source" is missing. Why isn't the "file_source" being p ...

Having trouble triggering the onclick event on a dynamically created table using JavaScript

I am facing an issue with adding a table programmatically that has an onclick event triggering a specific JavaScript function using the row's id for editing purposes. Unfortunately, the function is not being called as expected. I have attempted to mod ...

Notify users with a prompt when a modal or popup is closed on Google Chrome extensions

I have developed a Google Chrome extension for setting timers and receiving alerts. Currently, the alert only goes off when the extension is open, but I want it to fire even when the extension is closed. This extension currently requires the window to be ...

retrieve information from an array of objects that include promises

Within my react application, I am faced with the task of retrieving email and name data for various user IDs from separate API endpoints. To achieve this, I follow these steps: const promises = ids.map( id => ( {email: axios.get(`blabla/${id}/email ...

Reload iframe content using a .php file within a different iframe

I am currently working on a page that consists of 7 different iframes: <iframe id="leftframe" src="structure/leftbar.php"></iframe> <iframe id="headerframe" src="structure/header.php"></iframe> <iframe id="menuframe" src="struct ...

Utilizing Promises with Multiple .then() in JavaScript

I'm currently in the process of creating an array structure. In this structure, we have different parts and each part contains articles. What I've done is create a promise to gather all the parts using .then(), then I need to iterate through eac ...

Regular expression designed to validate a 19 digit MasterCard number

I need assistance with adjusting the regex pattern for Mastercard to support both 19 digit and 16 digit cards. Can someone please provide guidance on how to modify it? masterCardPattern: /^(?:5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0- ...

The process of retrieving matching strings from two collections in MongoDB

There are two collections: Collection A consists of: -> {"_id": .... , "data":"demon"} -> {"_id": .... , "data":"god"} and Collection B consists of: -> {"_id": .... , "tit ...

How to use jQuery to set a background image using CSS

I've been working on setting backgrounds dynamically with a jQuery script, but it seems like the .css function is not working as expected. Here's the code snippet: $(document).ready(function () { $(".VociMenuSportG").each(function () { ...

Using an iframe containing a link to trigger the opening of a colorbox in the

Recently, I encountered a challenge regarding an iframe containing a bar graph. I wanted to achieve that when the graph is clicked, it would open a colorbox with a more detailed graph from the "PARENT" of that iframe. Initially, I managed to get the ifram ...

The dictionary of parameters has an empty entry for the 'wantedids' parameter, which is of a non-nullable type 'System.Int32', in the 'System.Web.Mvc.JsonResult' method

The console is showing me an error stating that the parameters dictionary contains a null entry for parameter wantedids. I am trying to pass checked boxes to my controller using an array, so only the admin can check all boxes of tips for a specific user. T ...

The breakdown of an object literal in JavaScript

What is the reason that we cannot access the second item in the object literal the same way as the first? var foo = {a:"alpha",2:"beta"}; console.log(foo.a) -> printing 'alpha' correctly console.log(foo.2) -> Error: missing ) after argumen ...

Is it possible to switch from kilometers to miles on the distance matrix service in Google Maps?

let distanceService = new google.maps.DistanceMatrixService(); distanceService.getDistanceMatrix({ origins: [sourceLocation], destinations: [destinationLocation], travelMode: google.maps.TravelMode.DRIVING, unitSystem: google.maps.UnitSystem.IMPERI ...