开发者

Zend application jQuery ajax call getting error

I am trying to work with jQuery in Zend Framework. And the use case I am facing problem is when I am trying to save data to the db. Always receiving ajax error though the data is being saved in the database.

The controller that I am using to add data is like below:

public function addAction()
{
    // action body
    $form = new Application_Form_Costs();
    $form->submit->setLabel('Add');
    $this->view->form = $form;开发者_运维技巧

    if($this->getRequest()->isPost())
    {
        $formData = $this->getRequest()->getPost();
        {
            if ($form->isValid($formData))
            {
                $costTitle = $this->_request->getPost('costTitle');

                 $costAmount = $this->_request->getPost('costAmount');

                $costs  = new Application_Model_DbTable_Costs();

                if($costs->addCosts($costTitle, $costAmount))
                {
                    echo "suces";
                }

               // $this->_helper->redirector('index');
            }
            else
            {
                $form->populate($formData);  
            }
        }
    }
}

And the jQuery that is passing data is as follows:

  $('#cost').submit(function (){
      data = {
      "cost_title":"cost_title",
      "cost_amount":"cost_amount"
  };

  $.ajax({
            dataType: 'json',
            url: '/index/add',
            type: 'POST',
            data: data,

            success: function (response) {
             alert(response);
            },

            timeout: 13*60*1000,
            error: function(){
                alert("error!");
            }
        });
  });

I am getting always error.

What is the problem in this code?

Thanks in advance.


I would strongly recommend you implement the newest Zend/AJAX methods.

// Inside your php controller
public function init()
{
    $ajaxContext = $this->_helper->getHelper('AjaxContext');
    $ajaxContext->addActionContext('add', 'json')
                ->initContext();
}

public function addAction()
{
// action body

$form = new Application_Form_Costs();
$form->submit->setLabel('Add');
$this->view->form = $form;

if($this->getRequest()->isPost())
{
    $formData = $this->getRequest()->getPost();
    {
        if ($form->isValid($formData))
        {
            $costTitle = $this->_request->getPost('costTitle');
            $costAmount = $this->_request->getPost('costAmount');
            $costs  = new Application_Model_DbTable_Costs();
            if($costs->addCosts($costTitle, $costAmount))
            {
                // The view variables are returned as JSON.
                $this->view->success = "success";
            }
        }
        else
          {
          $form->populate($formData);  
        }
    }
}

// Inside your javascript file
// Assign handlers immediately after making the request,
  // and remember the jqxhr object for this request
  var jqxhr = $.get("/index/add/format/json", function(data) {
    alert(data);
  })
  .error(function() { alert("error"); });

For more information:

AjaxContext (ctrl+f)

jQuery.get()


I think you are getting an error on Session output. Why don't you disable the view-renderer, since you just need an answer for the request echo "suces" which is more than enough for your AJAX.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜