在我的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