javascript - ZF2-无法在 zend Framework 2 上使用 jQuery.ajax 访问 Controller 操作

标签 javascript ajax json jquery zend-framework2

为了设置两个依赖的下拉列表,我使用 jQuery.ajax,但我遇到了一些麻烦,我认为我在 $.ajax 上设置的 Controller 操作的 url 无法访问。

这是我的 Controller 操作代码:

 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>

希望这能为您提供更多详细信息。

最佳答案

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

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

        ),

关于javascript - ZF2-无法在 zend Framework 2 上使用 jQuery.ajax 访问 Controller 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18229057/

相关文章:

javascript - 如何将控制权交还给主函数 - jQuery、JS

javascript - Express Nodejs,模块化应用程序

javascript - 通过 API 定位当前的 Google 设备位置?

javascript - 通过 Javascript 将 PHP 数组传递给另一个 PHP 脚本

javascript - 无法解析 JSON 对象数组

javascript - 如何在页面中多次使用一个 SVG 文件

php - 我在哪里可以学习如何进行实时查询?

javascript - JS 字符串连接编码问题

javascript - 查询/搜索 JSON 中的值

java - Java 中的多个数组和对象 JSON