Reputation: 2029
im trying get a JSON data from the server via an AJAX call. The call works fine, but the success handler not get the correct JSON data generated by the server, instead, get all HTML content of the current page.
My PHP Code ($_POST['idFoto']
exists):
header('Content-type: application/json');
$fotos = FotoQuery::Create()->findByIdfoto($_POST['idFoto']);
if($fotos->count() != 1){
die("{success: false, msg: 'Error interno, foto no encontrada unívocamente.'}");
}
$foto = $fotos->getFirst();
$response = Array('success'=>true,'title'=>$foto->getTitulo(),'nombre'=>$foto->getNombre(),
'desc'=>$foto->getDescripcion(),'date'=>$foto->getFecha());
echo json_encode($response);
My Ajax call:
$.ajax({
url: document.domain +"/private/ajaxRequests/fotoRequestHandler.php",
method: "POST",
data: {idFoto: picId},
success: function(data,status,request) {
console.log(data);
console.log(status);
console.log(request);
}
});
The status call is "200 OK". Any ideas why this not work appropriately ?
UPDATE: When execute the scrip via URL (with a idFoto assigned manually) the json_encode
works fine:
{"success":true,"title":"Demo 1","nombre":"01.jpg","desc":"Descripci\u00f3n foto demo 1.","date":"07/24/13"}
But, i put die(json_encode($response))
at the end of script, Ajax Call continues reciving full HTML.
NOTE: Im use Smarty and friendly URLS, this have an impact on the problem ?.
Upvotes: 10
Views: 15564
Reputation: 1296
Try this. First clean buffer then print the json. At the end exit!
ob_clean();
echo json_encode($response);
die();
Upvotes: 2
Reputation: 2029
I found the problem, change the url property of the Ajax by "/private/ajaxRequests/fotoRequestHandler.php"
. And works fine !.
Upvotes: 3