如何在CakePHP中执行jquery ajax请求? [英] How do I perform a jquery ajax request in CakePHP?

查看:120
本文介绍了如何在CakePHP中执行jquery ajax请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在CakePHP中使用Ajax,但并没有真正实现目标!

I'm trying to use Ajax in CakePHP, and not really getting anywhere!

我的页面上有一系列按钮-单击其中一个按钮应在当前页面上显示特定内容.重要的是,不要重新加载页面,因为它将显示电影,并且我不希望重置电影.

I have a page with a series of buttons - clicking one of these should show specific content on the current page. It's important that the page doesn't reload, because it'll be displaying a movie, and I don't want the movie to reset.

有几个不同的按钮,每个按钮的内容都不相同;该内容可能很大,因此我不需要在需要之前将其加载.

There are a few different buttons with different content for each; this content is potentially quite large, so I don't want to have to load it in until it's needed.

通常我会通过jQuery来做到这一点,但是我无法在CakePHP中使用它.

Normally I would do this via jQuery, but I can't get it to work in CakePHP.

到目前为止,我有:

在视图中,按钮控件是这样的:

In the view, the button control is like this:

$this->Html->link($this->Html->image('FilmViewer/notes_link.png', array('alt' =>  __('LinkNotes', true), 'onclick' => 'showNotebook("filmNotebook");')), array(), array('escape' => false));

在此之下有一个名为"filmNotebook"的div,我希望在该div上显示新内容.

Below this there is a div called "filmNotebook" which is where I'd like the new content to show.

在我的functions.js文件中(在webroot/脚本中),我具有以下功能:

In my functions.js file (in webroot/scripts) I have this function:

function showNotebook(divId) {
 // Find div to load content to
 var bookDiv = document.getElementById(divId);
 if(!bookDiv) return false;
 $.ajax({ 
   url: "ajax/getgrammar",
   type: "POST",
   success: function(data) {
    bookDiv.innerHTML = data;
   }
  });
 return true;
}

为了生成将在div中显示的纯内容,我在routes.php中设置了以下内容:

In order to generate plain content which would get shown in the div, I set the following in routes.php:

 Router::connect('/ajax/getgrammar', array('controller' => 'films', 'action' => 'getgrammar'));

在films_controller.php中,函数getgrammar是:

In films_controller.php, the function getgrammar is:

  function getgrammar() {
   $this->layout = 'ajax';
   $this->render('ajax');
  }

布局文件仅具有:

当前,视图ajax.ctp只是:

and currently the view ajax.ctp is just:

<div id="grammarBook">
 Here's the result
</div>

问题在于,当我单击按钮时,我得到了默认的布局(就像页面出现在页面中一样),其中包含电影索引页面.好像在films_controller.php中找不到正确的动作

The problem is that when I click the button, I get the default layout (so it's like a page appears within my page), with the films index page in it. It's as if it's not finding the correct action in films_controller.php

我已经完成了CakePHP手册(http://book.cakephp.org/view/1594/Using-a-specific-Javascript-engine)中建议的所有操作.

I've done everything suggested in the CakePHP manual (http://book.cakephp.org/view/1594/Using-a-specific-Javascript-engine).

我做错了什么?我欢迎提出更好的方法的建议,但我也想知道Ajax应该如何工作,以供将来参考.

What am I doing wrong? I'm open to suggestions of better ways to do this, but I'd also like to know how the Ajax should work, for future reference.

感谢任何人都可以提供的帮助.

Thanks for any help anyone can offer.

推荐答案

您显示的所有内容似乎都不错.仔细检查ajax布局是否存在,因为如果不存在,将使用默认布局.在蛋糕中使用Firebug和日志功能检查一切是否按计划进行.

everything you show seems fine. Double check that the ajax layout is there, because if it's not there, the default layout will be used. Use firebug and log function in cake to check if things go as you plan.

更多建议:为什么需要将POST张贴到"ajax/getgrammar",然后将其重定向到"films/getgrammar"?然后渲染ajax.ctp视图?对我来说似乎是多余的.您可以将ajax调用为"films/getgrammar",并且不需要路由器规则.您可以将ajax.ctp更改为getgrammar.ctp,并且不需要$this->render('ajax');

A few more suggestions: why do you need to POST to 'ajax/getgrammar' then redirect it to 'films/getgrammar'? And then render ajax.ctp view? It seems redundant to me. You can make the ajax call to 'films/getgrammar', and you don't need the Router rule. You can change ajax.ctp to getgrammar.ctp, and you won't need $this->render('ajax');

这篇关于如何在CakePHP中执行jquery ajax请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆