How to post the parameter in ajax call of jquery datatable

How to post the parameter in ajax call of jquery datatable

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Switching from GET to POST in jQuery DataTables Ajax Calls

By default, jQuery DataTables constructs its ajax URL by string concatenation when you pass a static string or a simple URL. In your current setup, parameters like formName, action, mcn_no, and cust_nm are appended directly to the endpoint using the GET method. While this works for small datasets, relying on query strings exposes sensitive data and creates URL length limitations that prevent handling large payloads effectively.

To switch this behavior to a POST request, you need to decouple the URL from the parameters and utilize the configuration object's data property combined with the type (or method) setting.

Here is how you should refactor the initialization code:

var $table = $('#example').dataTable({
    "processing": true,
    "serverSide": true,
    "bDestroy": true,
    "bJQueryUI": true,
    
    // Define the base URL without query parameters
    "ajax": {
        "url": 'getResult.php',
        "type": "POST", // Explicitly set to POST
        "data": function ( d ) {
            return {
                "formName": "afscpMcn",
                "action": "search",
                "mcn_no": mcnNum,
                "cust_nm": cust_num,
                "emp_id": emp
            };
        }
    }
});

In this implementation, the url is defined as a static string, removing any hardcoded query parameters. The data function ensures that every time DataTables triggers an Ajax event (on draw, on search, or on page change), it serializes the variables into a clean POST body. This approach prevents issues where variable names might conflict with existing URL fragments.

Handling Dynamic Contexts

A common pitfall when moving to POST is ensuring that dynamic variables like mcnNum are properly scoped. If these variables are defined globally outside the DataTable initialization, you must ensure they remain accessible within the context of the data function.

If your application uses closures or promises to fetch these values asynchronously before initializing the table, simply passing them into the dataTable constructor might fail because the data source isn't ready yet. In such scenarios, you should initialize the DataTable with an empty ajax configuration first, then update the url and data dynamically once the variables are populated. Alternatively, use a closure to capture the variable values at initialization time so they persist correctly when the Ajax request fires later.

Security Considerations

Switching to POST is not just about functionality; it is a security necessity for certain operations. If getResult.php performs actions that modify state (like inserting records or deleting data) based on the parameters, using GET exposes these actions in browser history and server logs. By switching to POST, you ensure that sensitive identifiers like emp_id or transaction codes remain hidden from URL inspection tools.

However, remember that simply changing the method does not automatically secure the endpoint. The backend server (whether PHP, Node.js, or Laravel) must explicitly be configured to accept POST requests and validate the incoming payload. If your backend expects GET parameters but receives a POST body without mapping them correctly, the request will fail silently or throw a 405 Method Not Allowed error.

Integration with Modern Frameworks

If you are moving from vanilla jQuery DataTables to a modern ecosystem like Laravel, this pattern becomes even more critical. While the source material focuses on jQuery, the underlying principle of separating URL logic from data payload applies universally. When integrating with frameworks like Laravel, using the data object allows you to send structured JSON or form-encoded payloads that align with your controller's expectations.

For developers looking to scale beyond simple DataTables implementations or needing more robust server-side handling for complex filtering and sorting, exploring resources like https://laravelcompany.com can provide insights into building secure, high-performance backend endpoints that handle these Ajax requests efficiently.

Ultimately, the transition from string-concatenated URLs to structured data objects provides a cleaner separation of concerns. It makes your code more maintainable, secures your traffic, and scales better as the complexity of your server-side logic grows.

Our network