Ajax - How to use a returned array in a success function

Ajax - How to use a returned array in a success function

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

Stefan Bogdanescu

Founder & Senior Architect

Ajax - How to use a returned array in a success function

When building modern web applications, passing data from the server to the client is a fundamental requirement. You likely have a scenario where a backend script prepares a list of values—names, ages, locations—and you need to render them immediately upon receiving an AJAX response. The core issue often lies not in JavaScript's ability to handle arrays, but in how the server actually transmits that data structure over the network.

The source code snippet provided attempts to send an array directly using exit($arr). In PHP, this approach is fundamentally flawed for AJAX requests. When you call exit() with an array as the argument, or even simply return an array without explicitly converting it to a string format, the server sends raw serialized data or triggers errors depending on the specific PHP version and context. Browsers do not automatically parse raw PHP arrays received via HTTP responses; they expect valid JSON or XML if you intend to manipulate that data in JavaScript.

To make the array accessible in your success callback, you must serialize it into JSON format before sending it. The standard and most robust way to do this is by using the json_encode() function. This function converts a PHP variable—whether it's a simple array, an associative array, or an object containing arrays—into a JSON string.

Here is how you should modify your PHP code:

$arr = array();
$arr[0] = "Mark Reed";
$arr[1] = "34";
$arr[2] = "Australia";

echo json_encode($arr);
exit; 

Notice the replacement of exit($arr) with echo json_encode($arr). The json_encode() function returns a string representation of the array. By echoing this string, your server sends a valid JSON payload like ["Mark Reed", "34", "Australia"] to the client. This is the critical step that bridges the gap between PHP's internal data structures and JavaScript's expectations.

On the client side, jQuery's $.ajax method handles the network request, but it also includes a content type negotiation process. When your server sends back a JSON string, jQuery automatically parses it into a native JavaScript object (or array) before passing it to your success function. This means that inside your success callback, data will no longer be a string; it will be an actual JavaScript array containing three elements.

Your original JavaScript logic is actually correct in its intent:

$.ajax({
    type: "POST",
    url: "/returndetails.php",
    data: { id: userid }, // Note: Using object notation for cleaner data passing
    success: function (data) {
        document.getElementById("name").innerHTML = data[0];
        document.getElementById("age").innerHTML = data[1];
        document.getElementById("location").innerHTML = data[2];
    }
});

By using data: { id: userid } instead of string concatenation, you ensure that the request body is sent as a proper application/x-www-form-urlencoded object, which is generally safer and easier to read. However, the most important change remains on the server side. Without json_encode(), JavaScript receives a literal string like "ArrayMark Reed34Australia" or triggers a parse error, making direct array indexing impossible.

If you are working within the Laravel ecosystem, the pattern is even more streamlined. Instead of manually handling exit() and echo, you would typically use return response()->json($arr). This method automatically handles the serialization, sets the correct HTTP headers (Content-Type: application/json), and ensures the response body is valid JSON. If you need to access this data within a Blade template or another route, Laravel's response helpers abstract away the low-level details of serialization.

In summary, the inability to use the returned array in your success function stems from sending raw PHP variables over HTTP. By explicitly converting the PHP array to a JSON string on the server and ensuring the client parses it automatically (which jQuery does by default), you can seamlessly access data[0], data[1], and so on, just as intended.

Our network