[英]how to pass data from controller to jquery (Ajax) in codeigniter
controller code 控制器代码
$rates['poor'] = 10;
$rates['fair'] = 20;
$this->load->view('search_result2', $rates);
//I have tried this in many ways but at least It executes the "success" in ajax file only with above way. other ways I have tried ex :-
//$this->output->set_output(json_encode($rates));
//echo json_encode($rates);
i need to pass this rates array to ajax 我需要将此费率数组传递给ajax
js code js代码
$.ajax({
type:'POST',
url:'some url',
data:{'adID':adID},
//dataType:'JSON', // when I uncommented this it displays nothing. when commented it displays "undefined" on the label below i have created
success:function(rates){
$('#rate_val').html('<label>'+rates.poor+'</label>');
//I have tried this in many ways ex :-
// $('#rate_val').html('<label>'+rates['poor']+'</label>');
// $('#rate_val').html('<label>'+rates[0]+'</label>');
}
});
this displays "undefined" on label. 这会在标签上显示“未定义”。 I can't get the data I have passed from controller.
我无法获取从控制器传递来的数据。 please help ?
请帮忙 ?
dataType:'JSON'
dataType:'JSON'
echo
or set_output
from controller echo
或set_output
将输出设置为json rates.poor
or rates['poor']
from ajax rates.poor
或rates['poor']
Controller 调节器
public function post_url()
{
$rates = array();
$rates['poor'] = 10;
$rates['fair'] = 20;
$this->output->set_output(json_encode($rates));
}
Ajax 阿贾克斯
<script>
$.ajax({
type:'POST',
url:'POST_URL',
data:{'adID':adID},
dataType:'JSON',
success:function(rates){
$('#rate_val').html('<label>'+rates.poor+'</label>');
}
});
</script>
$data['a']='100';
$data['b']='200';
echo json_encode(array('success'=>$data));
jquery jQuery的
<script>
$.ajax({
type:'POST',
url:'POST_URL',
data:{'adID':adID},
dataType:'JSON',
success:function(rates){
var data = jQuery.parseJSON('['+response+']');
$('#rate_val').html('<label>'+data.succcess.a+'</label><label>'+data.succcess.b+'</label>');
}
});
</script>
i believe it may execute as you want to . 我相信它可以按您的意愿执行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.