I have a jqgrid that\'s functionning very well.
I was wondering is it possible to catch server sent errors ? how Is it done ?
If you're using jqGrid with the options
ajaxGridOptions: { contentType: 'application/json; charset=utf-8' },
datatype: "json",
url: wsPath
to load data via AJAX and web services or MVC controllers, then this answer is for you.
Note that if a runtime error occurs in the web method dealing with the AJAX call, it can't be catched via loadError, because loadError only catches HTTP related errors. You should rather catch the error in the web method via try ... catch
, then pass it in JSON format in the catch block using return JsonString
. Then it can be handled in the loadComplete event:
loadComplete: function (data) {
if (this.p.datatype === 'json') {
if (data!==undefined && data!==null && isErrorJson(data)) {
ShowErrorDialog(getJsonError(data));
}
// ...
}
The functions above have the following meaning, implement them as needed:
isErrorJson(data)
: returns true, if the data object contains error as defined in your web methodgetJsonError(data)
: returns the string with the error message as defined in your web methodShowErrorDialog(msg)
: displays the error message on screen, e.g. via jQueryUI dialog.In the web service method you can use JavaScriptSerializer
to create such an error object, for the 2 JavaScript methods above you can use the jQuery function $.parseJSON(data.d)
to get the message out of the JSON object.