csdn产品小助手 2013-03-06 07:08 采纳率: 0%
浏览 27

cakephp 2.x中的Ajax请求

I'm trying to send a simple ajax request in cakephp. I've to send ajax requests for many buttons. But I'm just trying a simple ajax request first. But it's not working for some reason. This is what I've done so far. This is my code.

show.ctp

<?php echo $this->Html->script('jquery', FALSE); ?>

<?php

echo $this->Form->create();
echo $this->Form->input('field', array('id'=>'field'));
echo $this->Js->submit('Send', array(
'before'=>$this->Js->get('#sending')->effect('fadeIn'),
'success'=>$this->Js->get('#sending')->effect('fadeOut'),
'update'=>'#success'
));

?>

<div id='sending' style="display:none"> Counz[dot]gif will be displayed </div>
<div id='success'></div>

controller

var $name = 'count';

    public $helpers = array('Js' => array('Jquery'));
    //var $helpers = array('Ajax', 'Javascript');
    var $components = array('RequestHandler');

public function show(){
        if($this->RequestHandler->isAjax()){
            $this->render('success', 'ajax'); //ajax tells the layout it should use

        }else{
            $this->set('for_map', $this->count->find('all'));

        }
    }

success.ctp

<p>It's working</p>

Also ajax.ctp is there by default.

Please tell me what am I doing wrong. Thanks. :)

  • 写回答

2条回答 默认 最新

  • weixin_33704591 2013-03-06 13:36
    关注

    It's better not to post to self when doing ajax requests.

    show.ctp

    echo $this->Form->create('Count', array('action'=>'ajaxShow'));
    

    controller

    public function ajaxShow(){
    
        $this->layout = 'ajax';
    
    
        if($this->RequestHandler->isAjax()){
             $this->render('success', 'ajax'); //ajax tells the layout it should use
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog