json parsing-zend框架中的错误

时间:2021-07-15 18:54:43

I want to make delete action asynchronous using AJAX, in the controller I make an action that handle the request

我想使用AJAX使删除操作异步,在控制器中我做了一个处理请求的操作

  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

并在jquery脚本中

 $("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.

我发现删除过程没有发生!!当我发送同步请求时,删除操作成功发生。而且当我在不使用布局页面的情况下进行AJAX调用时,删除也成功完成。

What can be the error?

可能是什么错误?

1 个解决方案

#1


0  

I suspect your layout is rendered and then is getting parsed (unsuccessfully) by the jQuery code. Try turning it off:

我怀疑你的布局是渲染的,然后被jQuery代码解析(失败)。尝试将其关闭:

try {
    $mapper->deleteVisit($visitId);
    if ($this->getRequest->isXmlHttpRequest()) {
        $this->_helper->json("1"); //this will disable layout and view renderer
                                   //and echo your data
    }
    else {
        //...
    }
}

#1


0  

I suspect your layout is rendered and then is getting parsed (unsuccessfully) by the jQuery code. Try turning it off:

我怀疑你的布局是渲染的,然后被jQuery代码解析(失败)。尝试将其关闭:

try {
    $mapper->deleteVisit($visitId);
    if ($this->getRequest->isXmlHttpRequest()) {
        $this->_helper->json("1"); //this will disable layout and view renderer
                                   //and echo your data
    }
    else {
        //...
    }
}