ZF2-Can't在zend框架2上使用jQuery.ajax访问控制器操作

ZF2- Can't access a controller action using jQuery.ajax on zend framework 2

本文关键字:jQuery ajax 访问 操作 控制器 访问控制 框架 zend ZF2-Can      更新时间:2023-09-26

为了设置两个依赖的下拉列表,我使用jQuery.ajax,但我遇到了一些问题,我认为我在$.ajax上设置的控制器操作的url是不可访问的。

这是我的控制器操作代码:

 public function fillIncidentsAction()
      {
            $request = $this->getRequest();
            if ($request->isPost())
            {
                $code_categ = (int) $request->getPost('code_categ',0);           
                $data = new JsonModel(array(
                    'success' => true,
                    'results' => $this->getTableInstance('TypeIncidentTable')
                                        ->getListTypeIncident($code_categ),
                ));
                return $data;
            }
        }

这是我的js函数的主要部分

$('#'+source).change(function() {
    if($('#'+source).val() != '')
     {
        $.ajax({
            type:  'POST',
            async: false,
            url:   url,
            cache: true,
            dataType: 'json',
            data:  { code_categ: $('#'+source).val() },
            success: function(data){
            alert(data.success);//<------ i added this to test if this function is executed or not
                if(data.success)
                {
                    $('#'+target).prop('disabled', true);
                    if(data.results != ""){
                        var options = new Array();
                        $.each(data.results, function(key, value){
                           options[((key) ? key : 0)] = '<option value="' + key + '">' + value + '</option>';
                        });
                        $("#"+target).html(options.join(''));
                        $('#'+target).prop('disabled', false);;
                    }
                }
            },

        });

我不知道我错在哪里了。有什么建议吗?谢谢你的帮助!

对不起,这是我设置网址的方式:

<script type="text/javascript">
 $(document).ready(function() {
  var url = "<?php echo $var =$this->url('gims/default', array('controller' =>       'evenement', 'action' => 'fill_incidents')); ?>";
// initialize the js function
dependentDropDown('firstList','secondList',url);
});
</script>

希望这能给你更多的细节。

问题出在我的moodule.config.php中,我还没有启用Json策略。

'view_manager' => array(
          //...
'strategies' => array(
                'ViewJsonStrategy',
        ),
        ),