I just wanted to check something here – do you have to echo values in PHP to retrieve them via (jQuery)XHR?
for example
PHP:
public function processRequest() {
//intercept AJAX requests
if (!empty($_GET)) {
if (isset($_GET['xhr'])) {
if ($_GET['xhr'] == true) {
//process AJAX
//call relevant method
echo json_encode(array('key' => 'value'));
return;
}
}
}
//else proceed with regular output
$this->render();
}
jQuery:
function doAjax(){
$.ajax({
url: "index.php?xhr=true",
cache: false,
dataType: 'json',
success: function(data){
console.log(data);
}
});
}
This logs a json object in Firebug console with the appropriate values.
If I try and return the value instead of echoing, I get nada.
They have to be echoed to be in the response, so yes they have to be there for jQuery to see them…otherwise the response that the browser gets won’t have the content.
As a side note, you can check for the
X-Requested-With: XmlHttpRequestheader if all you want is to check if it’s an AJAX request from jQuery, it already adds this header with every request, for example: