Ajax 来自 PHP 的请求不要 return 我一个数组
Ajax request from PHP don't return me an Array
我正在尝试从 Ajax 中的 PHP 脚本 return 一个数组,但是,它似乎不起作用,它 return 是一个字符串而是一个数组。我正在使用 CodeIgniter Framework,有我的 .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() 函数只是 select 全部来自数据库,但是我只使用温度和日期列。
它应该 return 我一个像 this one 这样的数组,
而是 return 是我 this.
你能帮我或给我一些线索吗?
此致。
您需要解析 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);
....
....
}
});
它应该可以工作。
试试这个
$.ajax({
url:"Cdonnees/get_form",
method:"GET",
success:function(data) {
$.each(data, function (key, value) {
console.log(key)
console.log(value)
})
}
});
我正在尝试从 Ajax 中的 PHP 脚本 return 一个数组,但是,它似乎不起作用,它 return 是一个字符串而是一个数组。我正在使用 CodeIgniter Framework,有我的 .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() 函数只是 select 全部来自数据库,但是我只使用温度和日期列。
它应该 return 我一个像 this one 这样的数组, 而是 return 是我 this.
你能帮我或给我一些线索吗?
此致。
您需要解析 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);
....
....
}
});
它应该可以工作。
试试这个
$.ajax({
url:"Cdonnees/get_form",
method:"GET",
success:function(data) {
$.each(data, function (key, value) {
console.log(key)
console.log(value)
})
}
});