首页 > 解决方案 > 为什么我在客户端上看不到错误异常?

问题描述

我有这个动作方法:

    public ActionResult SavePoint(PointRequest point)
    {
        //some logic
        
        throw new System.ArgumentException("Error, no point found");
    }
    
    and this ajax:
    
    function saveFeature(feature, callback, error) {
    $.ajax({
        url: window.homeUrl + "Feature/SavePoint",
        type: "GET",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: feature,
        success: callback,
        error: function (xhr, ajaxOptions, thrownError) {
                alert(thrownError);     
            }
        });
    }

    

在保存功能功能中,我想提醒异常文本:“错误,找不到点”。

但是 throwedError 是空的,知道为什么在 throwedError 中我看不到错误消息吗?

标签: javascriptc#ajaxmodel-view-controller

解决方案


Jaromanda X 和 Brett Caswell 在他们的评论中回答了“为什么”。但你可以这样做:

    public ActionResult SavePoint(PointRequest point)
    {
        try
        {
            //some logic
        }
        catch (Exception ex)
        {
            return BadRequest("Error, no point found");
        }
    }

或者,将实际错误包含给用户:

public ActionResult SavePoint(PointRequest point)
{
    try
    {
        //some logic
    }
    catch (Exception ex)
    {
        return BadRequest($"Error, no point found. Message: {ex.InnerException.Message}");
    }
}

推荐阅读