1
我有控制器稱爲Time
的Ajax在笨使用jQuery
<?php
class Time extends CI_Controller {
// just returns time
public function index() {
echo time();
}
}
?>
該控制器輸出的當前時間和由在視圖中使用以下代碼加載。
window.setInterval(function() {
$.get('time/index',
// when the Web server responds to the request
function(time) {
$('#mydiv').html(time+'<br/>');
}
)
}, 5000);
如可以看出只有HTML的響應只能使用,但如果我想Time
控制器返回數組,對象甚至是一個變量等我該怎麼辦呢?
你應該使用json_encode,前返回數組:返回json_encode($ timeArray);,然後ajax的部分,你應該解碼和遍歷數組,只要你想 –