本文介绍了jqgrid 服务器端错误消息/验证处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 json 响应中,我有状态"和错误"属性.如何在 jqGRid 中使用此错误属性.解析所有错误并在对话框中显示它们.

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

谢谢!

解决方案

您上一个问题的答案 我已经尝试过尝试回答您当前的问题.可能我表达的不够清楚.

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

网格中的加载数据、行的编辑以及与服务器的所有 Ajax 通信都是根据 HTTP 协议实现的.每个 HTTP 响应在响应的第一行都有状态代码.理解这句话的意思很重要.

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

HTTP/1.1 200 OK...内容类型:应用程序/json...{页面":1",....}

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

HTTP/1.1 404 未找到

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

演示有如下代码

$("#list").jqGrid({url: 'Unknown.json',//没有同名的文件数据类型:'json',//... 其他一些典型参数加载完成:函数(){警报(确定");},loadError: 函数 (jqXHR, textStatus, errorThrown) {alert('HTTP 状态码:' + jqXHR.status + '
' +'文本状态:' + 文本状态 + '
' +'错误抛出:' + 错误抛出);alert('HTTP 消息体 (jqXHR.responseText): ' + '
' + jqXHR.responseText);}});

显示如下所示的警报消息:

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

通过以上所有信息,我想向您展示错误响应和成功响应将由您使用的整个软件堆栈以另一种方式处理(jqGrid、jQuery、XMLHttpRequest 对象、...)所以你应该只使用 包含默认实现loadError(参见 这里here),在大多数 Ajax 错误的情况下生成相对可读的错误消息.它在错误 div 中显示结果文本,存在于网格主体上方.因此建议在使用自定义loadError 之前测试默认行为是否产生良好结果.如果您确实需要创建自己的 loadError 那么您可以使用免费 jqGrid 的 displayErrorMessage 方法将错误消息放在错误 div 中: $("#grid").jqGrid("displayErrorMessage", customErrorMessage);

In my json responses, I have 'STATUS' and 'errors' properties.How can use this errors properties with jqGRid. To parse all errors and show them in a dialog box.

Basically just check, if status:'ERROR' then display all errors.

Thanks!

解决方案

In the last part of the answer to your previous question I tried already tried to give the answer on your current question. Probably I expressed me not clear enough.

You should not place and information about the error inside of the standard successful response. You should just follow main rules of the HTTP protocol used for communication between the server and the client.

The loading data in the grid, editing of the rows and all Ajax communication with the server are implemented with respect of HTTP protocol. Every HTTP response has the status code in the first line of the response. It's very important to understand the meaning of this.

The typical successful request with JSON data looks as following

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

{"page":"1",....}

If the URL which try to load not exist for example the first line of the server response will be

HTTP/1.1 404 Not Found

and jqGrid based on the HTTP status code (404 in the case) *will not try to interpret the the server response as the data which contains data with the grid content.

The demo has the following code

$("#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 + '
' +
              'textStatus: ' + textStatus + '
' +
              'errorThrown: ' + errorThrown);
        alert('HTTP message body (jqXHR.responseText): ' + '
' + jqXHR.responseText);
    }
});

which display the alert message like the following:

Moreover in the jqXHR.responseText you will find the full body of the server response as as string. The next alert shows the response.

With all above information I wanted to show you that error responses and successful responses will be processed in another way by the whole stack of software which you use (jqGrid, jQuery, XMLHttpRequest object, ...). So you should just use error HTTP status codes in the server responses if the error will be detected. In the answer for example you will see how to do this in case of the usage ASP.NET MVC.

Here you can find another version of the loadError implementation which wait the input in the JSON form: {"Source":"some error source",Message:"Description of the error"}, and the error output will be like here

but the code can display additionally HTML response generated by your web server:

You can easy modify the code to your purpose. The code you can find below

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>'
    );
}

where the decodeErrorMessage function defined as

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.*?>([sS]*)</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>';
    };

UPDATE: Free jqGrid contains default implementation of loadError (see here and here), which generates relatively readable error message in case of the most Ajax errors. It displays the resulting text in the error div, existing above the body of the grid. Thus it's recommended to test, whether the default behavior produce good results before usage of custom loadError. If you really need to create your own loadError then you can place the error message in the error div using displayErrorMessage method of free jqGrid: $("#grid").jqGrid("displayErrorMessage", customErrorMessage);

这篇关于jqgrid 服务器端错误消息/验证处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-30 03:33
查看更多