【问题标题】:Handle error from MVC controller in jqGrid在 jqGrid 中处理来自 MVC 控制器的错误
【发布时间】:2015-06-03 08:08:33
【问题描述】:

如何在 MVC 中以 JSON 格式从服务器返回错误并在 jqGrid 中处理此错误? 在控制器中我使用这个

throw new Exception("message");

我在 jqGrid 中使用

loadError: Error
////

还有我的功能

function Error(xhr, st, err) {
            console.log(xhr.responseMessage);
        }

但是在 xhr.responseMessage 中有一个 html 代码,我只需要我的错误消息。

【问题讨论】:

    标签: jquery json asp.net-mvc jqgrid


    【解决方案1】:

    服务器端

    try {
        // some processing
    }
    catch (Exception e) {
        Response.StatusCode = (int)HttpStatusCode.BadRequest;
        return Json(new { Message = e.Message});
    }
    

    jqGrid

    loadError: function(xhr, status, error) {
        alert(xhr.responseText);
    }
    

    【讨论】:

      【解决方案2】:

      对 Wilts C 的回答稍作调整:

      服务器端

      try {
          // some processing
      }
      catch (Exception e) {
          Response.StatusCode = (int)HttpStatusCode.BadRequest;
          return Json(new { Message = e.Message});
      }
      

      jqGrid

      loadError: function(xhr, status, error) {
          alert(xhr.responseJSON.Message || "There was an unhandled problem!");
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-11-06
        • 1970-01-01
        • 2016-01-24
        • 2012-08-14
        • 2010-11-24
        • 2015-02-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多