jqgrid服务器端错误消息/验证处理

use*_*339 10 javascript jquery jqgrid

在我的json回复中,我有'STATUS'和'errors'属性.如何在jqGRid中使用此错误属性.解析所有错误并在对话框中显示它们.

基本上只需检查,如果状态:'ERROR'然后显示所有错误.

谢谢!

Ole*_*leg 14

在你上一个问题的答案的最后部分,我试过已经尝试在你当前的问题上给出答案.可能我表达的不够清楚.

您不应在标准成功响应中放置有关错误的信息.您应该遵循用于服务器和客户端之间通信的HTTP协议的主要规则.

网格中的加载数据,行的编辑以及与服务器的所有Ajax通信都是根据HTTP协议实现的.每个HTTP响应都响应的第一行中具有状态代码.了解这一点的意义非常重要.

使用JSON数据的典型成功请求如下所示

HTTP/1.1 200 OK
...
Content-Type: application/json
...

{"page":"1",....}
Run Code Online (Sandbox Code Playgroud)

如果尝试加载的URL不存在,例如服务器响应的第一行将是

HTTP/1.1 404 Not Found
Run Code Online (Sandbox Code Playgroud)

基于HTTP状态代码的 jqGrid (在这种情况下为404)*将不会尝试将服务器响应解释为包含具有网格内容的数据的数据.

该演示具有以下代码

$("#list").jqGrid({
    url: 'Unknown.json', // there are no file with the name
    datatype: 'json',
    // ... some other typical parameters
    loadComplete: function () {
        alert("OK");
    },
    loadError: function (jqXHR, textStatus, errorThrown) {
        alert('HTTP status code: ' + jqXHR.status + '\n' +
              'textStatus: ' + textStatus + '\n' +
              'errorThrown: ' + errorThrown);
        alert('HTTP message body (jqXHR.responseText): ' + '\n' + jqXHR.responseText);
    }
});
Run Code Online (Sandbox Code Playgroud)

它显示如下警告消息:

在此输入图像描述

此外,在jqXHR.responseText您将找到服务器响应的完整正文作为字符串.下一个警报显示响应.

通过以上所有信息,我想向您展示错误响应和成功响应将由您使用的整个软件堆栈(jqGrid,jQuery,XMLHttpRequestobject,...)以另一种方式处理.因此,如果检测到错误,您应该在服务器响应中使用错误的HTTP状态代码.在答案中,您将看到如何在使用ASP.NET MVC时执行此操作.

在这里,您可以找到另一个版本的loadError实现,它等待JSON格式的输入:{"Source":"some error source",Message:"Description of the error"},错误输出就像这里一样

在此输入图像描述

但代码可以显示您的Web服务器生成的HTML响应:

在此输入图像描述

您可以轻松修改代码以达到您的目的.您可以在下面找到代码

loadComplete: function () {
    // remove error div if exist
    $('#' + this.id + '_err').remove();
},
loadError: function (jqXHR, textStatus, errorThrown) {
    // remove error div if exist
    $('#' + this.id + '_err').remove();

    // insert div with the error description before the grid
    $(this).closest('div.ui-jqgrid').before(
        '<div id="' + this.id + '_err" style="max-width:' + this.style.width +
            ';"><div class="ui-state-error ui-corner-all" style="padding:0.7em;float:left;">' +
            decodeErrorMessage(jqXHR, textStatus, errorThrown) +
            '</div><div style="clear:left"/></div>'
    );
}
Run Code Online (Sandbox Code Playgroud)

其中decodeErrorMessage函数定义为

var decodeErrorMessage = function (jqXHR, textStatus, errorThrown) {
        var htmlBody, errorInfo, i, errorText = '',
            errorIconSpan = '<span class="ui-icon ui-icon-alert" style="float:left; display: inline-block; margin-right: .3em;"></span>';
        if (textStatus) {
            errorText = textStatus;
        }
        if (errorThrown) {
            if (errorText.length > 0) {
                errorText += '<hr/>';
            }
            errorText += errorThrown;
        }
        if (typeof (jqXHR.responseText) === "string") {
            if (jqXHR.responseText.charAt(0) === '[') {
                try {
                    errorInfo = $.parseJSON(jqXHR.responseText);
                    errorText = "";
                    for (i = 0; i < errorInfo.length; i += 1) {
                        if (errorText.length !== 0) {
                            errorText += "<hr/>";
                        }
                        errorText += errorInfo[i].Source + ": " + errorInfo[i].Message;
                    }
                } catch (e) { }
                errorText = errorIconSpan + errorText;
            } else {
                htmlBody = /<body.*?>([\s\S]*)<\/body>/i.exec(jqXHR.responseText);
                if (htmlBody !== null && htmlBody.length > 1) {
                    errorText = htmlBody[1];
                }
            }
        } else {
            errorText = errorIconSpan + errorText;
        }
        return '<div style="float:left">' + errorText + '</div>';
    };
Run Code Online (Sandbox Code Playgroud)

UPDATE:免费的jqGrid包含默认实现loadError(见这里这里),这在大多数Ajax错误的情况下产生相对可读的错误信息.它在错误div中显示结果文本,该文本存在于网格主体的上方.因此,建议在使用自定义之前测试默认行为是否产生良好结果loadError.如果你真的需要创建自己的,loadError那么你可以使用displayErrorMessagefree jqGrid的方法将错误消息放在错误div中:$("#grid").jqGrid("displayErrorMessage", customErrorMessage);