从 PHP 和 Ajax 返回 JSON 数据

在我的网络应用程序中,我只是尝试使用和查询JSONMySQL数据库返回数据。这是我遵循互联网上的教程的地方。万一在我的应用程序中它显示和错误类似;PHPAJAX

data = "↵↵↵
注意:未定义索引: 第29行C:\xampp\htdocs\Hospital\hospitalwebsite\test_query\fetch_count.php中的淋巴

这是我的 AJAX 代码:-

<script>

  $(document).ready(function () {

    $('select').material_select();


    $('#search').click(function () {

      var id = $('#test_list').val();

      if (id != '') {

        $.ajax({

          url: 'test_query/fetch_count.php', // Url to which the request is send

          method: 'POST', // Type of request to be send, called as method

          data: { id: id },


          //dataType:"JSON",

          success: function (data) {

            $('#success_mes').fadeIn().html(data);


            $('#test_info').css('display', 'block');


            $('#1').text(data.WBC);

            $('#2').text(data.lymph);

            $('#3').text(data.Mid);

          }

        });

      } else {

        alert('sdsd');

        $('#test_info').css('display', 'none');

      }

    });

  });

</script>

下面是 PHP 代码:-


<?php


session_start();

require_once "../phpquery/dbconnection.php";


if (isset($_POST['id'])) {


    //$id = $_POST['id'];


    $stmt = $con->prepare("SELECT * FROM testing_report WHERE testing_report_id = ? AND test_id='7' ");

    $stmt->bind_param("s", $_POST['id']);

    $stmt->execute();

    $result = $stmt->get_result();

    if ($result->num_rows === 0);

    while ($row = $result->fetch_assoc()) {


        $medRecords = json_decode($row['testing_results'], true);

        if (is_array($medRecords) || is_object($medRecords)) {

            foreach ($medRecords as $key => $object) {


                $data["WBC"] = $object['WBC'];

                $data["lymph"] = $object['lymph'];

                $data["Mid"] = $object['Mid'];

            }

        }

    }


    echo json_encode($data);

}

?>

SQL 架构

如果有人能帮助我,我真的很感激。谢谢


GCT1015
浏览 99回答 2
2回答

斯蒂芬大帝

问题是你的数据结构被分割成几个数组元素,比如......[&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "WBC": "1"&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "lymph": "5"&nbsp; &nbsp; }]所以数组的每个循环只有 1 条信息。此代码使用将所有数据组合成一组信息array_merge(),然后从结果中提取数据。我还添加了?? 0默认值 0(如果不存在),可能有更好的默认值。$data = [];$medRecords = json_decode($row['testing_results'], true);if (is_array($medRecords) || is_object($medRecords)) {&nbsp; &nbsp; $medRecords = array_merge(...$medRecords);&nbsp; &nbsp; $data["WBC"] = $medRecords['WBC'] ?? 0;&nbsp; &nbsp; $data["lymph"] = $medRecords['lymph'] ?? 0;&nbsp; &nbsp; $data["Mid"] = $medRecords['Mid'] ?? 0;}

慕桂英4014372

如果结果是 json,JQuery 工作文件:$(document).ready(function(){&nbsp; $('#search').click( function () {&nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; url: "https://reqres.in/api/users?page=2",&nbsp; &nbsp; &nbsp; method: "GET",&nbsp; &nbsp; &nbsp; success:function(data)&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log("page:", data.page);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(data);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; });});<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><button id="search">Search</button>我认为你必须在结果中添加正确的标题:<?phpheader('Content-Type: application/json');将此代码添加到您的 php 页面的第一行。然后 jQuery 知道结果是 json。
打开App,查看更多内容
随时随地看视频慕课网APP