我試圖從 Ajax 中的 PHP 腳本回傳一個陣列,但是,它似乎不起作用,它回傳一個字串而不是一個陣列。我正在使用 CodeIgniter 框架,有我的 .php 代碼:
public function get_form(){
$donneesModel = new Mdonnees();
$result = $donneesModel->getAll();
$data=array(
'chartDate' => array(),
'chartTemp' => array(),
);
foreach ($result as $row) {
$data['chartDate'][] = date("d/m", strtotime($row['date']));
$data['chartTemp'][] = $row['temperature'];
}
print json_encode($data);
}
還有我的 Ajax 請求:
$.ajax({
url:"Cdonnees/get_form",
method:"GET",
success:function(data) {
console.log(data);
....
....
}
我的 getAll() 函式只是從資料庫中選擇所有,但我只使用溫度和日期列。
它應該回傳給我一個像這樣的陣列,而是回傳給我這個。
你能幫我或給我一些關于發生了什么的線索嗎?
此致,。
uj5u.com熱心網友回復:
您需要在 Javascript 中決議 JSON:
$.ajax({
url:"Cdonnees/get_form",
method:"GET",
success:function(data) {
//this will print the data
console.log(JSON.parse(data));
// save it to a variable and can use it in the rest of the program
const pareseData = JSON.parse(data);
....
....
}
});
它應該可以作業。
uj5u.com熱心網友回復:
試試這個
$.ajax({
url:"Cdonnees/get_form",
method:"GET",
success:function(data) {
$.each(data, function (key, value) {
console.log(key)
console.log(value)
})
}
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/417684.html
標籤:
下一篇:函式名會影響性能嗎?
