You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel项目AJAX调用无法从数据库获取数据仅显示N/A如何解决

问题原因及修复方案

1. 控制台报错Uncaught ReferenceError: $ is not defined修复

该报错是因为jQuery库未在AJAX代码执行前加载,按以下步骤处理:

  • 在包含fetchSummary函数的页面中,将jQuery引入代码放在所有JS代码之前,示例引入代码:
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  • 避免JS代码在DOM未加载完成时执行,可将fetchSummary函数包裹在$(document).ready()中。

2. 视图仅显示N/A的修复

jQuery加载完成后如果仍显示N/A,按以下顺序排查:

第一步:验证查询逻辑正确性

在控制器fetchSummary方法中加入打印语句验证查询结果是否为空:

public function fetchSummary(Request $request){
    // 先验证传入参数是否正确
    dd($request->IdPatient);
    $FollowupTreatment=DB::table("Treatment")
       ->join("Treatment_Followup","Treatment_Followup.treatmeant_id","Treatment.IdTreatment")
       ->join("Followup","Followup.IdFollowup","Treatment_Followup.follow_up")
       ->where('numerosettimana',4)
       ->where("Treatment.IdPatient",$request->IdPatient)
       ->get();
    // 验证查询结果是否为空
    dd($FollowupTreatment);
    return view('user_view.PatientTreatmentSummary',compact('FollowupTreatment'));
}

发起请求后检查响应内容,常见查询错误点:

  • 关联字段拼写错误:Treatment_Followup.treatmeant_id疑似拼写错误,正确应为treatment_id
  • numerosettimana=4的条件不符合实际存储的数据,无匹配记录
  • 传入的IdPatient参数为空或与数据库存储的患者ID不匹配

第二步:优化视图遍历逻辑

原视图固定循环6次,若查询结果长度小于6则后续项默认显示N/A,可优化为空值判断逻辑:

@php
// 提取对应字段为数组
$minorComplications = $FollowupTreatment->pluck('complicanzeminori')->toArray();
$majorComplications = $FollowupTreatment->pluck('complicanzemaggiori')->toArray();
@endphp
<tr>
   <th  class="table-info">Complicanze Minori</th>
   @for($i=0;$i<6;$i++)
      <td class="data-{{$i+1}}">
         {{ $minorComplications[$i] ?? 'N/A' }}
      </td>
   @endfor
</tr>
<tr>
   <th  class="table-info">Complicanze Maggiori</th>
   @for($i=0;$i<6;$i++)
      <td class="data-{{$i+1}}">
         {{ $majorComplications[$i] ?? 'N/A' }}
      </td>
   @endfor
</tr>

内容的提问来源于stack exchange,提问作者Dixit Soni

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 17:09:02