Haciendo jquery ajax llamada desde la vista al controlador en cakephp 2.x
controller cakephp-2.3 (1)
/*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*/
Estoy tratando de hacer una solicitud de ajax desde la vista al controlador, ajax requst está funcionando bien, pero desde el controlador no se devuelve nada a la vista. No sé dónde está el problema. Lo que intento es, en mi opinión, mostrar algunos datos del controlador y hay un cuadro de selección. Cuando selecciono una ciudad del cuadro de selección, llama a la solicitud de ajax y debe mostrar el resultado de esa ciudad en particular en 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);
}
});
});
});
y en la vista
<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
}
}?>
Y en el controlador
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'');
}
}
}