How can the datatype be specified in AJAX requests to properly parse JSON data returned from PHP?

To properly parse JSON data returned from PHP in AJAX requests, the datatype needs to be specified as 'json' in the AJAX request. This tells the AJAX function to expect JSON data in the response and automatically parse it into a JavaScript object. By setting the datatype to 'json', you ensure that the data is correctly formatted and can be easily accessed and manipulated in your JavaScript code.

<?php
// PHP code that returns JSON data
$data = array('name' => 'John Doe', 'age' => 30);
echo json_encode($data);
?>