在我的json响应中,我有'状态'和'错误'属性。 如何使用这个错误属性与jqGRid。解析所有错误并在对话框中显示它们。jqgrid服务器端错误消息/验证处理
基本上只是检查,如果状态:'错误',然后显示所有错误。
谢谢!
在我的json响应中,我有'状态'和'错误'属性。 如何使用这个错误属性与jqGRid。解析所有错误并在对话框中显示它们。jqgrid服务器端错误消息/验证处理
基本上只是检查,如果状态:'错误',然后显示所有错误。
谢谢!
前段时间我在找一个类似的问题,碰到这个answer。 阅读Oleg的回答。他是jqgrid的人;-)
在你上一个问题的the answer的最后部分,我试过已经试过给你当前的问题提供答案。可能我表达得不够清楚。
您不应该在标准成功响应中放置错误信息。您应该遵循用于服务器和客户端之间通信的HTTP协议的主要规则。
网格中的加载数据,行的编辑以及与服务器的所有Ajax通信都是针对HTTP协议实现的。每个HTTP响应在响应的第一行中都有状态码。理解这个意义是非常重要的。
与JSON数据的典型的成功请求看起来如下
HTTP/1.1 200 OK
...
Content-Type: application/json
...
{"page":"1",....}
如果该尝试加载例如不存在于服务器响应的第一行中的URL将是
HTTP/1.1 404 Not Found
和的jqGrid 基于HTTP状态码(在这种情况下为404)*不会尝试将服务器响应解释为包含网格内容数据的数据。
The demo具有下面的代码
$("#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);
}
});
这显示类似如下的警告消息:此外,在
的jqXHR.responseText
你会发现服务器响应的全身如字符串。下一个警报显示响应。
有了以上所有的信息,我想告诉你错误响应和成功的响应将以另一种方式由你使用的整个软件栈(jqGrid,jQuery,XMLHttpRequest
object,...)处理。因此,如果检测到错误,您应该在服务器响应中使用error HTTP status codes。例如,在the answer中,您将看到如何在使用ASP.NET MVC的情况下执行此操作。
Here你可以找到loadError
实现其等待输入的JSON形式的另一个版本:{"Source":"some error source",Message:"Description of the error"}
,错误输出将喜欢这里
但代码可以显示另外的HTML响应由Web服务器生成:
您可以轻松修改代码以您的目的。该代码可以在下面找到
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>'
);
}
其中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>';
};
UPDATE:Free jqGrid包含默认实现loadError
(见here和here),其产生相对可读错误消息在Ajax错误最多的情况下。它显示错误div中的结果文本,位于网格正文的上方。因此建议测试,在使用自定义loadError
之前,默认行为是否产生良好结果。如果你真的需要创建自己的loadError
,那么你可以将错误消息放在错误div使用displayErrorMessage
免费方法jqGrid:$("#grid").jqGrid("displayErrorMessage", customErrorMessage);
谢谢!你是对的。 [答案](http://stackoverflow.com/questions/4290821/jqgrid-server-exception-error-messages/4291444#4291444)另外描述了在编辑模式中使用的错误。 – Oleg