【问题标题】:How do I display the string message from BadRequest(message) using Ajax如何使用 Ajax 显示来自 BadRequest(message) 的字符串消息
【发布时间】:2021-09-09 11:23:29
【问题描述】:

我有我的 API 控制器

public ActionResult<Campaigns> AddCampaign([Bind("Name, Venue, AssignedTo, StartedOn, CompletedOn")] Campaigns campaigns)
    {
        try
        {
            if (ModelState.IsValid)
            {
                if (CampaignNameExists(campaigns.Name))
                {
                    throw new Exception("A campaign with that name already exists!");
                }
                if(!UsersIdExists(campaigns.AssignedTo))
                {
                    throw new Exception("User assigned to this Campaign does not exist!");
                }

                var isAdmin = _accessCheck.AdminCheck(campaigns.AssignedTo);

                //Check if user is Admin
                if(isAdmin.IsAdmin == 1)
                {
                    throw new Exception("Admins cannot be assigned to campaigns");
                }

                //Send User details to Repo for insertion to DB
                if (condition)
                    return result
                else
                    throw new Exception("Could not add campaign");
            }
            else
            {
                throw new Exception("Model State is not valid");
            }
        }
        catch (Exception ex)
        {
            var message = ex.Message;
            return BadRequest(message);
        }
    }

我的观点

    <script>
    $(document).ready(function () {

        $("#createBtn").click(function () {
            let data = {
                "Name": $('#Name').val(),
                "Venue": $('#Venue').val(),
                "AssignedTo": $('#AssignedTo').val(),
                "StartedOn": $('#StartedOn').val(),
                "CompletedOn": $('#CompletedOn').val()                
            };
            $.ajax({
                type: "POST",
                url: "http://localhost:25733/api/Admin/Campaign/Add",
                data: JSON.stringify(data),
                contentType: "application/json",
                success: function () {
                    document.getElementById("campDivSuccess").style.display = 'block'
                },
                error: function (response) {
                    document.getElementById("campDivFail").style.display = 'block'
                    let fail = '<p>'+ response +'</p>'
                    $('#campDivFail').append(fail);
                }
            })
        })
    })
</script>

应该在这个div中添加BadRequest(string)作为Paragraph标签

<div class="alert alert-dismissible alert-danger" id="campDivFail" style="display: none">
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>

它正在做什么,但作为 [object] [Object] 而不是作为我分配给 BadRequest 的字符串,比如说“无法分配广告系列”。我搜索了很多,但找不到解决方案。我使用响应错误吗?有没有其他功能可以做到这一点?

【问题讨论】:

  • 调试的时候,你的AJAXerror回调中response变量的实际内容是什么?听起来您正在寻找该对象的特定属性,而不是整个对象本身。
  • @David 感谢您的回复,我一提出这个问题,就进入了一个兔子洞并找到了正确的文档。我正在寻找的内容是“responseText”。我只需要在段落中使用 response.responseText。消息来源:developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest

标签: javascript jquery ajax asp.net-web-api asp.net-ajax


【解决方案1】:

我找到了正确的文档。我在“响应”中寻找的内容是“响应文本”。我只需要在段落中使用 response.responseText。来源:developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 2019-02-20
    • 2020-01-07
    • 1970-01-01
    • 2019-09-21
    • 2015-02-05
    • 1970-01-01
    相关资源
    最近更新 更多