【问题标题】:How to call a CakepPHP 3 action from JQuery如何从 JQuery 调用 CakepHP 3 动作
【发布时间】:2017-02-01 12:50:45
【问题描述】:

我正在尝试使用 JQuery UI 构建一个自定义对话框,我需要这个对话框是一个带有两个按钮的通用对话框(允许我从许多视图中加载它)。我想向它发送控制器名称、动作名称和参数,当用户按下 OK 按钮时,我调用控制器动作传递参数但我不知道如何实现。

我在构建对话框时没有问题,问题来自尝试从 JQuery 调用控制器,我尝试过类似的方法但它不起作用:

function showDialog(controller, action, params){
    $( function() {
        $( "#main-dialog" ).dialog({
        modal: true,
        buttons: {
            Ok: function() {
                $.ajax({ url: "http://" + domain.name + "/" + controller + "/" + action + "/" + params });
                $( this ).dialog( "close" );
            },
            Cancel: function(){
                $( this ).dialog( "close" );
            }
        }
        });
    } );
}   

【问题讨论】:

  • 你能把你收到的错误贴出来
  • 我没有收到任何错误,只是链接不起作用,Javascript控制台说域属性不存在,但我尝试手动添加完整的url,没有任何区别。跨度>

标签: jquery cakephp controller call action


【解决方案1】:

我终于这样解决了:

function showDialog(controller, action){
    var url = "http://" + document.domain + "/" + controller + "/" + action;
    // ...Loop through the received parameters and attach them to the "url" (using the required separator)
    $( function() {
        $( "#main-dialog" ).dialog({
            modal: true,    
            buttons: {
                Ok: function() {
                    window.open(url,"_self");
                    $( this ).dialog( "close" );
                },
                Cancel: function(){
                    $( this ).dialog( "close" );
                }
             }
        });
    });
}

【讨论】:

    【解决方案2】:

    试试这个

    url  = '<?php echo $this->html->url(array('controller'=>'YourController','action'=>'YourAction')) ?>'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多