You are right that errorTextFormat is the correct way to get server response in case of HTTP errors and display the corresponding error message.
First of all your server have to return response with an HTTP error code in the HTTP header. Then you should define your implementation of the errorTextFormat event handle either as a part of prmEdit
, prmAdd
, prmDel
parameters of the navGrid or you can overwrite jqGrid default settings (see here). I personally prefer to set errorTextFormat by modifying of jQuery.jgrid.edit
and jQuery.jgrid.del
. An example of the corresponding code you can find in the following old answer.
The exact code of errorTextFormat function should depend on the format of the server response. I use ASP.NET MVC with WFC inside of the site and the server can return either JSON encoded string response (if the error come from throw new WebFaultException<string> ("my error text", statusCode);
which I thrown explicitly) or sometime HTML response. In my implementation of errorTextFormat I test which kind of error response I received and convert the server response. Here is the code fragment:
my.errorTextFormat = function (data) {
var str = data.responseText.substr(0, 1);
var str1 = data.responseText.substr(0, 6).toLowerCase();
if (str === '"') {
var errorDetail = jQuery.parseJSON(data.responseText);
var s = "Fehler: '";
s += data.statusText;
s += "'. Details: ";
s += errorDetail;
return s;
}
else if (str1 === "<html " || str1 == "<html>" ||
data.responseText.substr(0, 169) === '<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html ') {
var bodyHtml = /<body.*?>([sS]*)</body>/.exec(data.responseText)[1];
return bodyHtml; //bodyContents1;
}
else {
var res = "Status: '";
res += data.statusText;
res += "'. Felhercode: ";
res += data.status;
return res;
}
};
jQuery.extend(jQuery.jgrid.edit, {
...
errorTextFormat: my.errorTextFormat
});
jQuery.extend(jQuery.jgrid.del, {
...
errorTextFormat: Testportal.errorTextFormat
});
The code is not perfect, but you can use it to create your own one.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…