【问题标题】:Error with ajax and zf2 controller actionajax 和 zf2 控制器操作出错
【发布时间】:2014-08-26 09:15:09
【问题描述】:

我试图使用 ajax 重定向到 zend 框架 2 中的控制器操作,但 ajax 没有正确响应,并且我没有收到数据警报。

这里是ajax代码:

$(".save_btn").click(function (){ //the class of submit button is save_btn
  $.ajax({
    type : 'POST',
    url : '/template/addtemplate',
    data : {'id':'test'},
    success : function(data,status)
    {
        alert(data.message);
    }
  });
});

这是我的控制器代码:

public function addtemplateAction()
{
    $result = array('status' => 'error', 
                    'message' => 'There was some error. Try again.'
              );
    $request = $this->getRequest();

    if($request->isXmlHttpRequest()){

        $data = $request->getPost();

        if(isset($data['id']) && !empty($data['id'])){

        return new JsonModel($result);

            $result['status'] = 'success';
            $result['message'] = 'We got the posted data successfully.';
        }
    }
    return new JsonModel($result);    
}

我还在我的 module.config.php 文件中添加了这些特殊的东西:

'strategies' => array (
               'ViewJsonStrategy'
                ),

我认为问题在于 $request->isXmlHttpRequest() 返回空白。

任何帮助都将被接受..

【问题讨论】:

    标签: ajax zend-framework2


    【解决方案1】:

    使用任何类型的开发人员工具。 Chrome => f12 => 网络选项卡并检查您的回复

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-10
      • 2017-10-02
      • 2014-01-01
      • 2016-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-26
      相关资源
      最近更新 更多