Zend Framework:如何使用 ajax 调用函数(而不是控制器操作)?
假设我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议为其编写一个操作。如果
test()
内部存在您需要其他操作才能使用的逻辑,则将其排除。让它成为自己的操作有几个原因:
请记住,并非每个操作都必须是其自己的完整页面。要确保您做的一件事是禁用此操作中视图的自动渲染,这样它就不会抱怨找不到它。
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:
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.
就这样想吧;)
just think about this way ;)