【问题标题】:Display exception message from server on ajax error在ajax错误时显示来自服务器的异常消息
【发布时间】:2012-11-28 16:45:07
【问题描述】:

我使用以下代码在客户端显示来自服务器的异常消息:

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public static void test(String text)
    {
               try
               {
                    throw new Exception("Hello");
               }

               catch (Exception ex)
               {
                HttpContext.Current.Response.Write(ex.Message);
                throw new Exception(ex.Message, ex.InnerException);
               }

    }

客户端:

    $.ajax({
        url: 'DownloadFile.aspx/test',
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        // Pass the value as JSON string                 
        // You might need to include json2.js for the JSON.stringify
        //method: 'http://www.json.org/json2.js',
        async: false,
        dataType: "json",
        data: '{ "text": "' + text'"}',
        success: function(Result) {

        },

        error: function(xhr, textStatus, errorThrown) {
            var err = eval("(" + xhr.responseText + ")");
            alert(err.Message);
        }
    });

当我使用 localhost 时,我会在警报弹出窗口中看到“Hello”。 当我在远程服务器上使用相同的代码时,出现一般系统错误。

如何获取向用户显示的异常消息文本?

【问题讨论】:

  • “一般系统错误”是什么意思?是否调用了您的 jQuery 错误处理程序?
  • @Heather - 是的,正在调用 jquery 错误处理程序,但我收到 responseText 消息:“处理请求时出错”

标签: c# ajax jquery


【解决方案1】:

您需要在 web.config 中设置 <customErrors mode="Off" />

您可以阅读更多here

一般来说,您似乎有 mode="RemoteOnly",它仅向 localhost 显示详细的异常消息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-26
    • 1970-01-01
    • 2019-07-02
    • 2020-09-17
    • 2010-09-27
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多