Javascript cakephp中的Ajax未按预期呈现页面
我是ajax/javascript/jquery新手,正在尝试将页面呈现到包含我的表单的页面上。我正在让它渲染,但当它渲染时,它会转到我的表单页面,除非它实际加载页面,忽略布局并删除我的表单 这是我的控制器代码:Javascript cakephp中的Ajax未按预期呈现页面,javascript,php,jquery,ajax,cakephp,Javascript,Php,Jquery,Ajax,Cakephp,我是ajax/javascript/jquery新手,正在尝试将页面呈现到包含我的表单的页面上。我正在让它渲染,但当它渲染时,它会转到我的表单页面,除非它实际加载页面,忽略布局并删除我的表单 这是我的控制器代码: <?php class PlayersController extends AppController{ public $components = array('RequestHandler'); public function listall() {
<?php
class PlayersController extends AppController{
public $components = array('RequestHandler');
public function listall() {
$this->set ( 'players', $this->Player->find ( 'all' ) );
}
public function index() {
if (!empty($this->request->data)) {
$this->Player->create();
if ($this->Player->save ( $this->request->data )){
$this->set ( 'players', $this->Player->find ( 'all' ));
$this->render('listall', 'ajax');
}
}
}
}?>
我一直在试图弄清楚如何在视图索引中实际呈现视图列表,而不实际重新加载页面。现在不知所措 很难理解你所提供的信息到底在做什么。试试这个- 在您的控制器中
<?php
class PlayersController extends AppController{
public $components = array('RequestHandler');
public function listall() {
$this->layout = 'ajax';
$this->set ( 'players', $this->Player->find ( 'all' ) );
}
public function index() {
if (!empty($this->request->data)) {
$this->Player->create();
if ($this->Player->save ( $this->request->data )){
$this->Session->setFlash('Congrats');
}
}
}
}?>
谢谢,不过它的作用是呈现索引视图,因此我最终得到两个表单。我想做的是:我的表单上现在有一个所有玩家的列表(listall),我想在列表中添加一个玩家,然后在索引中呈现所有玩家的列表。因此,当我使用js save而不是submit时,基本上会将listall视图呈现到index视图上。
<p style="background-color: lightblue">Players</p>
<table>
<tr>
<td>Name</td>
</>tr
<?php foreach ($players as $players): ?>
<tr>
<td><?php echo h($players['Player']['name']); ?> </td>
</tr>
<?php endforeach; ?>
</table>
echo $this->Html->script('jquery-2.1.0.min');
echo $this->fetch('meta');
echo $this-Js->writeBuffer(array('cache' => TRUE));
<?php
class PlayersController extends AppController{
public $components = array('RequestHandler');
public function listall() {
$this->layout = 'ajax';
$this->set ( 'players', $this->Player->find ( 'all' ) );
}
public function index() {
if (!empty($this->request->data)) {
$this->Player->create();
if ($this->Player->save ( $this->request->data )){
$this->Session->setFlash('Congrats');
}
}
}
}?>