Zend Framework:如何使用 ajax 调用函数(而不是控制器操作)?

发布于 2024-11-26 21:26:48 字数 559 浏览 1 评论 0原文

假设我在 IndexController 中有一个名为 test() 的公共函数:

public function test(){
    //some code here
}

在 index.phtml 视图文件中,我想使用 JQUERY AJAX 来调用 test() 函数,但对此一无所知。

代码:

<a href="javascript:void(0)" onclick="callTestFunction()">Click me to call test() function()</a>
<script>
callTestFunction = function(){
    $.ajax({
        type: "POST",
        Url: ***//WHAT SHOULD BE HERE***
        Success: function(result){
            alert('Success');
        }
    });
}
</script>

Assume that I have a public function in IndexController called test():

public function test(){
    //some code here
}

In index.phtml view file, I want to use JQUERY AJAX to call test() function but have no idea about this.

Code:

<a href="javascript:void(0)" onclick="callTestFunction()">Click me to call test() function()</a>
<script>
callTestFunction = function(){
    $.ajax({
        type: "POST",
        Url: ***//WHAT SHOULD BE HERE***
        Success: function(result){
            alert('Success');
        }
    });
}
</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美人如玉 2024-12-03 21:26:48

我建议为其编写一个操作。如果 test() 内部存在您需要其他操作才能使用的逻辑,则将其排除。

让它成为自己的操作有几个原因:

  • 您可以直接测试操作的结果,而不必通过代理。
  • 您可以利用上下文切换,具体取决于您是否需要返回 JSON 或 HTML。
  • 它是您尝试通过 AJAX 执行的操作,因此无需隐藏它。

请记住,并非每个操作都必须是其自己的完整页面。要确保您做的一件事是禁用此操作中视图的自动渲染,这样它就不会抱怨找不到它。

I would suggest writing an action for it. If there is logic inside of test() that you need other actions to be able to use, the factor that out.

There are a couple of reasons for having it be its own action:

  • You can test the results of the action directly instead of having to go through a proxy.
  • You can take advantage of context switching depending on if you need JSON returned or HTML
  • It is an action that you are trying to hit via AJAX, so there's no need to hide it.

Remember that not every action has to be its own full fledged page. One thing to make sure you do is disabling the auto-rendering of the view inside this action so it doesn't complain about not being able to find it.

樱娆 2024-12-03 21:26:48
public function ajaxproxyAction(){
    if (is_callable($_REQUEST['function_name'])) {
        return call_user_func_array($_REQUEST['function_name'], $_REQUEST['function_params'])
    }
}


<script>
callTestFunction = function(){
    $.ajax({
        type: "POST",
        Url: '/controller/ajaxproxy/',
        data: { function_name: 'echo', function_params: 'test' }
        Success: function(result){
            alert('Success');
        }
    });
}
</script>


public function ajaxproxy2Action(){
    if (method_exists($this, $_REQUEST['function_name'])) {
        $retval = call_user_func_array(array($this, $_REQUEST['function_name']), $_REQUEST['function_params']);
        echo json_encode(array('function_name' => $_REQUEST['function_name'], 'retval' => $retval));
        die;
    }
}

就这样想吧;)

public function ajaxproxyAction(){
    if (is_callable($_REQUEST['function_name'])) {
        return call_user_func_array($_REQUEST['function_name'], $_REQUEST['function_params'])
    }
}


<script>
callTestFunction = function(){
    $.ajax({
        type: "POST",
        Url: '/controller/ajaxproxy/',
        data: { function_name: 'echo', function_params: 'test' }
        Success: function(result){
            alert('Success');
        }
    });
}
</script>


public function ajaxproxy2Action(){
    if (method_exists($this, $_REQUEST['function_name'])) {
        $retval = call_user_func_array(array($this, $_REQUEST['function_name']), $_REQUEST['function_params']);
        echo json_encode(array('function_name' => $_REQUEST['function_name'], 'retval' => $retval));
        die;
    }
}

just think about this way ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文