I want to make delete action asynchronous using AJAX, in the controller I make an action that handle the request
public function deleteAction() {
$request = $this->getRequest();
if ($request) {
$visitId = $request->getParam('visit_id');
$mapper = new Visits_Model_VisitsMapper();
try {
$mapper->deleteVisit($visitId);
echo json_encode("1");
} catch (Exception $e) {
$this->view->message = "error inserting Data, Error details: " . $e->getMessage();
echo json_encode("0");
}
Zend_Controller_Front::getInstance()->setParam('noViewRenderer', true);
}
}
and in the jquery script
$("a.delete").live('click',function(e){
e.preventDefault();
var data={
"visit_id":$(this).parent().parent().attr("id")
};
jQuery.ajax({
url: "/visits/visit/delete",
type: "POST",
dataType: 'json',
data: requestDate,
success: successCallback,
error:failureCallback
});
When I click on delete button, the request is sent and an error on pop up occur
Error <!-- application/layouts/scripts/layout.phtml -->
the whole html of layout page....
error SyntaxError: JSON.parse: unexpected character
I detect that the delete process does not happened!!
When I send synchronous request the delete operation happened successfully. And also when I make the AJAX call without using Layout page the delete is succeeded successfully also.
What can be the error?
I suspect your layout is rendered and then is getting parsed (unsuccessfully) by the jQuery code. Try turning it off: