Php 如何通过我的后端页面中的循环获取响应数据
我有一个问题,如何选择所有的数据通过循环在我的后端页面?然后在我的前端,我想使用jquery的each函数将所有数据追加到相应的类中。我已经创建了一个函数,状态是200,正确吗?因此,当我单击响应callback.js:31时,它们会在console.log错误中指向我。为什么会发生这种情况?我的状态是200?为了更好地理解,我将向你们分享我的代码 callback.js:31{readyState:4,getResponseHeader:ƒ, getAllResponseHeaders:ƒ,setRequestHeader:ƒ,OverrideMemimetype:ƒ,…} 要点问题:如何在我的jquery中通过循环获取数据。 Ajax:Php 如何通过我的后端页面中的循环获取响应数据,php,jquery,Php,Jquery,我有一个问题,如何选择所有的数据通过循环在我的后端页面?然后在我的前端,我想使用jquery的each函数将所有数据追加到相应的类中。我已经创建了一个函数,状态是200,正确吗?因此,当我单击响应callback.js:31时,它们会在console.log错误中指向我。为什么会发生这种情况?我的状态是200?为了更好地理解,我将向你们分享我的代码 callback.js:31{readyState:4,getResponseHeader:ƒ, getAllResponseHeaders:ƒ,s
$.ajax({
url:'./Function/fetch_mission_vision.php',
type:'get',
success:function(response) {
$.each(response,function(i,el){
console.log(el.title);
});
},
error:function(error) {
console.log(error);
}
})
<?php
require_once('../ConnectionString/require_db.php');
session_start();
//submit function
$status = 'Active';
$posttype_mission = 'mission';
$posttype_vision = 'vision';
$about_psmid_content = $db->prepare('SELECT title,content FROM tblcontent
WHERE status = ? AND posttype = ? OR posttype = ? ORDER BY contentID DESC LIMIT 2')
or die($db->error);
$about_psmid_content->bind_param('sss',$status,$posttype_mission,$posttype_vision);
$about_psmid_content->execute();
$about_psmid_content->bind_result($title,$content);
while ($about_psmid_content->fetch()) {
$data = ['title'=>$title,'content'=>$content];
header('Content-type: application/json');
echo json_encode($data);
}
?>
Php:
$.ajax({
url:'./Function/fetch_mission_vision.php',
type:'get',
success:function(response) {
$.each(response,function(i,el){
console.log(el.title);
});
},
error:function(error) {
console.log(error);
}
})
<?php
require_once('../ConnectionString/require_db.php');
session_start();
//submit function
$status = 'Active';
$posttype_mission = 'mission';
$posttype_vision = 'vision';
$about_psmid_content = $db->prepare('SELECT title,content FROM tblcontent
WHERE status = ? AND posttype = ? OR posttype = ? ORDER BY contentID DESC LIMIT 2')
or die($db->error);
$about_psmid_content->bind_param('sss',$status,$posttype_mission,$posttype_vision);
$about_psmid_content->execute();
$about_psmid_content->bind_result($title,$content);
while ($about_psmid_content->fetch()) {
$data = ['title'=>$title,'content'=>$content];
header('Content-type: application/json');
echo json_encode($data);
}
?>
如何使用所有数据构建一个数组,然后在循环后对其进行json编码
while ($about_psmid_content->fetch()) {
$data[] = ['title' => $title,'content' => $content];
}
header('Content-type: application/json');
echo json_encode($data);
移动
echo json\u encode($data)代码>在while循环之外它可以工作,但我只得到1个数据。我的数据超过1我需要循环它