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

.NET-MVC中评论增删改JS无响应问题排查求助

.NET MVC新闻公告板评论增删改功能故障排查

问题说明

基于.NET MVC开发的新闻公告板,从API拉取文章正常,但评论区的增删改功能完全失效:

  • 尝试将JS代码放在脚手架JS目录、HTML前后、脚本标签、_Layout.cshtml中均无效果
  • 开发者工具无报错,仅提示「Response should include x-type header」
  • 评论操作按钮仅登录时显示(此逻辑正常),但点击添加/编辑/删除按钮无任何反应

需要实现的核心功能:

  • 登录用户可添加评论
  • 点击编辑按钮拉取对应评论内容修改
  • 点击删除按钮移除目标评论

当前使用的JS代码

document.addEventListener("DOMContentLoaded", function () {
    let isEditMode = false;
    let currentCommentId = null;

    const articleDiv = document.querySelector('.article');
    const articleId = articleDiv ? articleDiv.getAttribute('data-article-id') : null;
    const myModal = new bootstrap.Modal(document.getElementById('addCommentModal'));

    Array.from(document.querySelectorAll(".add-comment-btn")).forEach(function (btn) {
        btn.addEventListener("click", function () {
            isEditMode = false;
            currentCommentId = null;
            document.getElementById('commentContent').value = '';
            myModal.show();
        });
    });

    Array.from(document.querySelectorAll(".edit-comment-btn")).forEach(function (btn) {
        btn.addEventListener("click", function () {
            isEditMode = true;
            currentCommentId = this.getAttribute("data-comment-id");

            $.get(`/Home/EditComment?commentId=${currentCommentId}`, function (data) {
                document.getElementById('commentContent').value = data.commentContent;
            });

            myModal.show();
        });
    });

    Array.from(document.querySelectorAll(".delete-comment-btn")).forEach(function (btn) {
        btn.addEventListener("click", function () {
            const commentId = this.getAttribute("data-comment-id");

            $.post("/Home/DeleteComment", { commentId: commentId }, function (response) {
                if (response.success) {
                    const commentDiv = document.querySelector(`[data-comment-id="${commentId}"]`).closest(".comment");
                    commentDiv.remove();
                    myModal.hide(); 
                    location.reload();
                }
            });
        });
    });

    document.getElementById('saveCommentBtn').addEventListener('click', function () {
        const commentContent = document.getElementById('commentContent').value;

        if (isEditMode) {
            $.post("/Home/EditComment", { commentId: currentCommentId, newContent: commentContent }, function (response) {
                if (response.success) {
                    myModal.hide(); 
                    location.reload();
                }
            });
        } else {
            if (articleId) {
                $.post("/Home/AddComment", { articleId: articleId, content: commentContent }, function (response) {
                    if (response.success) {
                        myModal.hide(); 
                        location.reload();
                    }
                });
            } else {
                console.error("Article ID is not available.");
            }
        }
    });

    // Hide modal on close button
    document.querySelector("[data-dismiss='modal']").addEventListener('click', function () {
        myModal.hide(); 
    });
});

排查与修复方案

  1. 修复JS语法错误
    原代码末尾缺失两个闭合的});,导致整个脚本无法执行,已在上方代码中补全。

  2. 验证依赖加载
    代码中使用了$.get/$.post(jQuery)和Bootstrap Modal,需确认这两个库已正确引入:

    • 打开开发者工具Network面板,检查jQuery和Bootstrap的JS文件是否加载成功
    • 在控制台输入$和bootstrap,确认返回对应对象而非undefined
  3. 改用事件委托处理动态元素
    若评论按钮是登录后动态渲染的,初始DOMContentLoaded时按钮不存在,事件绑定会失效。替换为事件委托:

    // 替换添加评论按钮的绑定逻辑
    document.body.addEventListener('click', function(e) {
        if (e.target.classList.contains('add-comment-btn')) {
            isEditMode = false;
            currentCommentId = null;
            document.getElementById('commentContent').value = '';
            myModal.show();
        }
    });
    

    编辑、删除按钮同理,通过父元素委托监听点击事件。

  4. 检查后端响应头
    「Response should include x-type header」提示说明后端接口返回的响应头不符合要求,需确保接口返回Content-Type: application/json,否则jQuery无法正确解析AJAX响应。可在Network面板查看请求的响应头确认。

  5. 监控AJAX请求状态
    点击按钮时,在Network面板查看是否有对应的GET/POST请求发送:

    • 无请求:事件未绑定成功,回到步骤2、3排查
    • 有请求:查看响应状态码(如404/500)和返回内容,排查后端接口逻辑

内容的提问来源于stack exchange,提问作者Dennis Mariegaard Jensen

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 19:31:34