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

ASP.NET Core MVC中jQuery Ajax渲染JSON数据返回undefined问题求助

解决方案

你的问题由两处核心错误导致,按以下步骤修正即可:

问题1:数据遍历层级错误

后端GetNoteById接口返回的是Course课程对象,你需要的通知列表存储在Course对象的Notices关联属性中,前端直接遍历返回的顶级Course对象,自然取不到通知的Id、Name等字段。

补充注意:ASP.NET Core 默认JSON序列化会将C#大驼峰属性转为小驼峰,因此前端取数组时要写data.notices而非data.Notices。

问题2:模板字符串语法错误

代码中两处插值表达式漏掉了模板字符串必须的$前缀,会导致变量不被解析,直接输出原文。


修正后的Ajax代码

只需要修改success回调中的逻辑即可:

success: function (data) {
    noteTable.html("");
    var result = "";
    // 改为遍历关联的通知列表,而不是整个Course对象
    $.each(data.notices, function (id, note) {
        console.log(id);
        console.log(note);

        result += `
            <tr>
                <td>${note.id}</td>
                <td>
                    <a href="/note/NoteDetail/${note.id}" class="nav-link mt-0">${note.name}</a>
                </td>
                <td>
                    <a href="/note/DowloadNoteFromDB/${note.id}" class="nav-link mt-0">###</a>
                </td>
                <!-- 补全$符号 -->
                <td>${note.fileType}</td>
                <td>${note.createdOn}</td>
            </tr>`
    });
    // 可选添加空数据提示
    if(result === "") {
        result = `<tr><td colspan="5" class="text-center">当前课程暂无通知</td></tr>`
    }
    noteTable.html(result);
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.13 14:24:03