How to get "data" from JQuery Ajax requests

How to get "data" from JQuery Ajax requests

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

Stefan Bogdanescu

Founder & Senior Architect

Decoding AJAX Data Transmission in PHP and jQuery

When you execute a jQuery $.ajax call, the browser constructs an HTTP request based on the parameters provided in your JavaScript object. In your specific example, you are sending a string payload via a POST request to test.php. The line data: "check" instructs jQuery to serialize this value into the request body. By default, jQuery sets the request method to POST and appends the data as an application/x-www-form-urlencoded parameter named check with the value check.

To access this information on the server side using PHP, you must inspect the superglobal arrays that hold incoming HTTP data. The most direct approach is checking the $_POST array. Since your jQuery code sends a simple key-value pair without specifying a custom content type or serialization format, the data will arrive in $_POST['check'].

<?php
// test.php
if (isset($_POST['check'])) {
    $receivedData = $_POST['check'];
    
    // Output the received data
    echo $receivedData; 
} else {
    // Handle cases where data is missing or sent via GET instead of POST
    http_response_code(400);
    echo "No 'check' parameter found in POST data.";
}
?>

This implementation relies on the standard behavior of jQuery's AJAX serialization. When you pass a string directly to the data property, jQuery does not treat it as raw JSON or XML; it treats it as form fields. Consequently, expecting $request->input('check') (Laravel style) or accessing json_decode(file_get_contents('php://input')) would result in null values unless you explicitly change the client configuration.

Understanding Content-Type and Serialization Trade-offs

A critical pitfall in AJAX development is assuming how the server will interpret the input based solely on what the developer intends. If you later decide to send complex objects or JSON arrays, simply changing the PHP code to read $_POST without adjusting the jQuery data option or the contentType setting will break the data flow.

For instance, if you want to send a JSON object like { "check": true }, your jQuery code must change:

$.ajax({
    type: "POST",
    url: 'test.php',
    contentType: 'application/json', // Explicitly set content type
    data: JSON.stringify({ check: true }), // Send raw JSON string
    success: function(data){
        alert(data);
    }
});

In this scenario, test.php would need to read from php://input or parse the application/json header to extract the values correctly. This distinction is vital because $_POST only captures data when the request body is encoded as application/x-www-form-urlencoded or multipart/form-data.

Handling Cross-Origin and Security Considerations

While not explicitly in your source code, it is worth noting that sending raw strings or form data via AJAX often occurs in single-page applications where cross-origin resource sharing (CORS) might be a concern. If test.php resides on a different domain than the HTML file, the browser will block the request unless the server sends the appropriate Access-Control-Allow-Origin header.

Additionally, always validate incoming data before processing it. In your example, the value is "check", but in a production environment, user input could contain malicious scripts or unexpected formats. Using htmlspecialchars() when echoing the data back to the browser helps prevent Cross-Site Scripting (XSS) vulnerabilities.

// Safer output
$echoData = htmlspecialchars($_POST['check'], ENT_QUOTES, 'UTF-8');
echo $echoData;

Framework Integration Context

If you are migrating this logic into a modern PHP framework like Laravel, the syntax changes to match the framework's request abstraction layer. Instead of $_POST['check'], you would use $request->input('check'). However, the underlying HTTP protocol remains identical: the server still receives the POST body with the key check and value check. Understanding this bridge between vanilla PHP superglobals and framework request objects ensures that your API logic remains consistent regardless of the environment.

For developers looking to deepen their understanding of building robust APIs with Laravel, resources like https://laravelcompany.com offer further insights into handling requests securely within a larger ecosystem.

Our network