我已成功获取有关php native的控制器数据并在视图中显示 像这样的代码:
控制器
function help(){
$data['result'] = array("name" => "jason", "age" => 20);
$this->load->view('profiles', $data); }
并像这样查看
foreach ($result as $key) {
echo $key;
}
结果将显示 " jason 20"
问题是如何使用ajax执行,而不加载url?
答案 0 :(得分:1)
返回一个JSON对象
$this->output->set_content_type('application/json');
$this->output->set_output(json_encode( $result ));`enter code here
然后在视图中使用jquery
调用控制器 `$.ajax({
type: "POST",
url: "<?php echo base_url('index.php/controller')?>",
data: {some_val: val},
success: function (data) {
$.each(data, function (key, value) {
console.log(value.name);
console.log(value.age);
}
});