Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ajax return full html page

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 ?.

like image 809
ramiromd Avatar asked Nov 14 '13 18:11

ramiromd


1 Answers

I found the problem, change the url property of the Ajax by "/private/ajaxRequests/fotoRequestHandler.php". And works fine !.

like image 115
ramiromd Avatar answered Nov 09 '22 00:11

ramiromd