I have a jqgrid that's functionning very well.
I was wondering is it possible to catch server sent errors ? how Is it done ?
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 look at the jqgrid demo site and look at "What's new in version 3.2" There should be a section about controlling server errors.
Specifically, it uses a callback parameter loadError:
As mcv states above, some errors are data errors, so you'll need to handle those specifically.
Use the callbacks. If you get an actual http error (a 400 or 500, for example), loadError(xhr, status, error) is triggered.
But some errors (like validation) shouldn't throw a 400 or 500 error. But you can still catch those in loadComplete(xhr). Parse your json, and check for whatever way you're using to identify errors. For example, I'm doing this in my loadComplete():
if (jsonResponse.errors) { $.each(jsonResponse.errors, function(i, val){ addErrorMessage($("#"+val.field), val.message); }); }
Another thing to remember/ or that I found is that if you are using Asp.net you need to turn
in the section - this will allow you to introspect the Message coming back as well.
If you're using jqGrid with the options
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 usingreturn JsonString
. Then it can be handled in the loadComplete event: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.You can use the
loadError
event in jqGrid definition (see documentation). E.g.:I have recently made extensive use of jqgrid for a prototype project I am working on for CB Richard Ellis (my employer). There are many way to populate a jqgrid, as noted in the documentation: (see the "retrieving data" node).
Currently I make a service call that returns a json string that when evaluated, gives me an object that contains the following:
In my success callback, I manually create the jqgrid like this: ("data" is the object I get when evaluating the returned json string).
So you can see I manually populate the data. There is more than 1 kind of server error. There is the logical error, which you could return as a property in your json string, and check before you try to create a jqgrid (or on a per-row basis).
Or on a per-row basis
If your error is an unhandled exception on the server, then you'll probably want an error callback on your async call. In this case, your success callback that (presumably) is creating your jqgrid won't be called at all.
This, of course, applies to manually populating a jqgrid, which is only one of the many options available. If you have the jqgrid wired directly to a service call or a function to retrieve the data, then that's something else entirely.
On the documentation page, look under Basic Grids > Events. There you'll see the "loadError" event that might come in handy.