【问题标题】:How to get the response data throught looping in my backend page如何通过在我的后端页面中循环获取响应数据
【发布时间】:2019-06-06 23:52:57
【问题描述】:

我有一个问题,如何在我的后端页面中通过循环选择所有数据?然后在我的前端,我想使用 jquery 的每个函数来附加相应类中的所有数据。我已经创建了一个函数,状态是 200,哪个是正确的?因此,当我单击响应 callback.js:31 时,他们将我指向 console.log 错误,为什么会发生我的状态为 200?为了更好地理解,我将与你们分享我的代码。

callback.js:31 {readyState: 4, getResponseHeader: ƒ, getAllResponseHeaders: ƒ, setRequestHeader: ƒ, overrideMimeType: ƒ, ...}

要点问题:如何在我的jquery中通过循环获取数据。

阿贾克斯:

$.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:

    <?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);
   }



?>

【问题讨论】:

  • echo json_encode($data);移出while循环
  • 是的,它有效,但我只得到 1 个数据。我的数据超过 1 我需要循环它

标签: php jquery


【解决方案1】:

用你的所有数据构建一个数组,然后在循环后对其进行 json 编码呢?

  while ($about_psmid_content->fetch()) {
     $data[] = ['title' => $title,'content' => $content];
  }
  header('Content-type: application/json');
  echo json_encode($data);

【讨论】:

    猜你喜欢
    • 2021-04-29
    • 1970-01-01
    • 2021-07-03
    • 1970-01-01
    • 1970-01-01
    • 2016-01-25
    • 1970-01-01
    • 1970-01-01
    • 2016-07-04
    相关资源
    最近更新 更多