【问题标题】:Send error message from webmethod从 webmethod 发送错误消息
【发布时间】:2011-07-11 06:40:53
【问题描述】:

如何从 webmethod 获取错误消息,我想启动错误。另外,我希望针对不同的场合提供不同的错误消息。

        $.ajax({
            type: "POST",
            url: "betting.aspx/submitFunction",
            data: JSON.stringify({ results: jsonObj, edit: $("#ctl00_ContentPlaceHolder1_CreateOrEdit").val() }),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(arg) { //call successfull
                window.location = "bHistory.aspx";
            },
            error: function(xhr) {
                alert("Error! You need to login, and try again");
                window.location = "login.aspx";
                //error occurred
            }
        });

【问题讨论】:

    标签: jquery asp.net ajax


    【解决方案1】:

    你可以试试这个:

    error: function(msg, status) {
        if (status === "error") {
            var errorMessage = $.parseJSON(msg.responseText);
            alert(errorMessage.Message);
        }
    }
    

    【讨论】:

      【解决方案2】:

      我也遇到过这个问题。问题是如果你从你的网络方法中抛出一个异常,它会被包装,就像"Message":"{your message}","StackTrace":" at ...

      我最终做的是创建这个 js 函数来解析返回的异常。
      它非常丑陋,我很想看看是否有人提出了更好的办法。

      function getErrorMessage(e) {
          return e.responseText.substring(e.responseText.indexOf("\"Message\":\"") + "\"Message\":\"".length, e.responseText.indexOf("\",\"Stack"));
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-25
        • 1970-01-01
        • 1970-01-01
        • 2021-05-08
        • 1970-01-01
        • 2018-12-15
        相关资源
        最近更新 更多