I have some problem with Zend Ajax. That is my js code:
function deleteNewsCategory(cId) {
var conf = confirm("Are you sure you want to delete the item?");
if(conf) {
$.ajax({
dataType: 'json',
url: '/ajax/deletenewscategory',
type: 'POST',
data: {
cId : cId
},
success: function (response) {
alert(response);
}
});
}
}
That is my AjaxController:
public function init() {
$this->_helper->ajaxContext->addActionContext('deletenewscategory', 'json')
->initContext();
}
public function deletenewscategoryAction() {
if ($this->getRequest()->isPost()) {
echo $this->_request->getPost('cId');
}
}
I add deleteNewsCategory function in submit button, but when I click this button and appear confirm message and click ok I don’t see anything?
There is either a Javascript error preventing the submission from happening, or the response doesn’t contain JSON data so your handler isn’t being called.
Do you have a view script with the
ajax.phtmlsuffix since you are using the Ajax context?See if adding
exit;after your echo statement in thedeletenewscategoryActionresults in the alert showing up.The next thing would be to use Firebug or livehttpheaders for Firefox, or Wireshark to sniff out the HTTP request and response to see what data you are getting back.
Edit: Since you get a 404 error, try:
By using the
url()helper it will construct a URL according to your routes and base path.The reason your alert isn’t showing up is because the 404 contains HTML and not valid JSON, jQuery isn’t calling your success handler.