在cakephp 2.x中使jquery ajax从视图调用到控制器 [英] Making jquery ajax call from view to controller in cakephp 2.x

查看:241
本文介绍了在cakephp 2.x中使jquery ajax从视图调用到控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个ajax请求从视图到控制器,ajax requst工作正常,但从控制器什么也不返回到视图。
我不知道在哪里的问题..
我想要的是在我的视图侧,我显示一些数据从控制器,并有一个选择框。从选择框中选择一个城市,它会调用ajax请求,并在view.ctp中显示特定城市的结果。

  $('#cityid')。change(function(){

$ city_id = $('#cityid:selected')。 city_id);

$ .ajax({
url:<?php echo Router :: url(array('controller'=>'deals','action'=> ;'topdeals'),true);?>,
type:POST,
cache:false,
data:{city_id:city_id},
success: function(data){
alert(data);

}
});

});

});

并在视图中

 < div id =form> 
<?php echo $ this-> Form-> create('Deal',
array('action'=>'topdeals','type'=>'post') );δ>
<?php
echo $ this-> Form-> input('city_id',
array('label'=>'City','type'=& 'select',
'id'=>'city_id','empty'=>'select City','options'=> $ city)
echo $ this-> Form-> end();
?>

< / div>

< div class =line>< / div>


<?php
if(!empty($ topdealsortbyrank))
{

foreach($ topdealsortbyrank)
{?>
< div class =items>

< div class =itemslogo>
<?php echo $ this-> Html-> image('deal / dealimage /'.$ topdealsortbyrank ['Deal'] ['image'],array('width'=>100px ,'height'=>80px));?>
< / div><! - items Logo ends - >


< div class =itemdetails>
< b><?php echo $ topdealsortbyrank ['Advertiser'] ['name']?>< / b>
< p class =red><?php echo $ topdealsortbyrank ['Deal'] ['title']?>< / p&
<?php
}
}?>

并在控制器中

  function topdealajax()

{
$ this-> log('Ajax call ---------------- - ');
if($ this-> request-> isAjax())
{
$ this-> log('inside if request is ajax --------- --------');
$ this-> layout = null;
$ this-> view ='topdeals';

if(!empty($ this-> request-> data)){
$ this-> log('inside如果不是空的params ------ -----------');
$ data = $ this-> request-> data ['city_id'];
$ this-> log($ data);
$ city_id = $ data ['city_id'];
$ this-> log($ city_id);
$ city_id = $ this-> request-> data ['city_id'];
// $ this-> log($ city_id);
$ topDealSortbyRank1 = $ this-> Deal-> find('all',array('conditions'=> array('date_expiry> ='=> date('Ymd'),
'date_expiry< ='=>'date_expiry','Deal.city_id'=> $ city_id),'order'=> array('Deal.deal_rank ASC')));
// $ this-> log($ topDealSortbyRank1);
$ this-> set('topdealsortbyrank',$ topDealSortbyRank1);
$ this-> render('topdeals');
}
}
}


解决方案>

  / *在你应该有的AJAX请求* / 
$ .ajax({
.....
success:function数据){$('#MYDIV')。html(data);},//您可以用响应
...
}附加或替换容器的内容);

//在DealsController :: topdeals()中你应该在开始时
if($ this-> request-> isAjax()):
$ this- > layout = null;
$ this-> view ='view_ajax'; //其他视图不需要布局,只有必要时
endif;

/ *为你想要的任何地方,发送到视图,视图获得修改并返回结果* /


I Am trying to make an ajax request from the view to controller, ajax requst is working fine, but from controller nothing is returned back to the view. i don't know where is the problem.. what i am trying is in my view side i am displaying some data from the controller and there is one select box. whe i select a city from the select box, it calls the ajax request and should show the result from that particular city in the view.ctp.

           $('#cityid').change(function() {

   $city_id= $('#cityid :selected').val();
   alert($city_id);

    $.ajax({
              url     : "<?php echo Router::url(array('controller' => 'deals', 'action' =>'topdeals'), true); ?>",
              type    : "POST",
              cache   : false,
              data    : {city_id: city_id},
              success : function(data){
              alert(data);

              }
          });

        });

     });

and in the view

            <div id="form">
             <?php echo $this->Form->create('Deal',
             array('action'=>'topdeals','type'=>'post'));?>
              <?php
             echo $this->Form->input('city_id',
        array('label'=>'City','type'=>'select',
       'id'=>'city_id','empty'=>'select City','options' =>$city));
             echo $this->Form->end();
               ?>

            </div>

            <div class="line"></div>


              <?php
          if(!empty($topdealsortbyrank))
             {

              foreach($topdealsortbyrank as $topdealsortbyrank)
                 {?>
         <div class="items">

            <div class="itemslogo" >
             <?php echo $this->Html->image('deal/dealimage/'.$topdealsortbyrank['Deal']['image'],array('width'=>"100px",'height'=>"80px"));?>
            </div><!-- items Logo ends-->


            <div class="itemdetails">
              <b><?php echo $topdealsortbyrank['Advertiser']['name']?></b>
    <p class="red"><?php echo $topdealsortbyrank['Deal']['title']?></p>
                <?php
                     }
                       }?>

And in the controller

           function topdealajax()

    {
        $this->log('Ajax call -----------------');
        if ($this->request->isAjax())
        {
            $this->log('inside if request is ajax -----------------');
          $this->layout = null;
            $this->view = 'topdeals';

           if(!empty($this->request->data)) {
            $this->log('inside if  not empty of params -----------------');
            $data = $this->request->data['city_id'];
            $this->log($data);
            $city_id=$data['city_id'];
            $this->log($city_id);
            $city_id= $this->request->data['city_id'];
       // $this->log($city_id);
       $topDealSortbyRank1=$this->Deal->find('all',     array('conditions'=>array('date_expiry >=' =>date('Y-m-d ') ,
                    'date_expiry <=' => 'date_expiry','Deal.city_id'=>$city_id),'order'=>array('Deal.deal_rank ASC')));
               //$this->log($topDealSortbyRank1);
                $this->set('topdealsortbyrank',$topDealSortbyRank1);
                 $this->render('topdeals');
                }
                     }
                     }

解决方案

/*IN THE AJAX REQUEST YOU SHOULD HAVE*/
$.ajax({
.....
success: function(data){ $('#MYDIV').html(data);}, //YOU CAN APPEND OR REPLACE THE CONTENT OF A CONTAINER WITH THE RESPONSE
...
});

//in DealsController::topdeals() you should have at the begining
if ($this->request->isAjax()):
        $this->layout = null;
        $this->view = 'view_ajax'; //Other view that doesn't needs layout, only if necessary 
    endif;

/*DO WHATEVER YOU WANT HERE, SEND IT TO THE VIEW, THE VIEW GETS RENDERD AND RETURN AS A RESULT*/

这篇关于在cakephp 2.x中使jquery ajax从视图调用到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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