I’m working on implementing a uniform error handler for remote cfc methods following Ben Nadel’s model. Before getting to this point, I was just returning the data object to jqGrid in the form that it expects. Now, I’m returning a response object that looks like so:
RESPONSE.SUCCESS = "true/false"
RESPONSE.ERRORS = [array of errors]
RESPONSE.DATA = {normal data object that jqGrid expects}
So, I now need to massage the response object when it returns so that I have a
RESPONSE.SUCCESS == "true"
handler that, at the very least, maps RESPONSE.DATA to the data object that jqGrid expects, and also a
RESPONSE.SUCCESS == "false"
handler that does something else. My first thought was to try to implement the beforeProcessing option. However, if I add this to my initialization code:
beforeProcessing: function(data, status, xhr) {
console.log(data + ' beforeProcessing was called');
},...
nothing ever gets logged, either on initialization or on subsequent calls. Also, if I revert to my original code and successfully reload the grid, nothing ever runs inside
beforeProcessing: function(){
alert('hi');
console.log('hi there');
},...
which would seem to indicate that beforeProcessing is broken.
If I can’t get this to work, I’m going to try to implement ajaxGridOptions{} but can’t figure out exactly what needs to be set in there – I haven’t found the documentation to be much help.
I think there are a misunderstanding about the usage of Ajax and loading of grid from the server. The Ajax use HTTP protocol internally which divide clear successful responds from the error responds. If the server detect any kind of error it should set error HTTP status (see here or here). In
case of HTTP status code 400 and higher the
loadErrorcallback will be called instead ofloadComplete. The format of the error response can be absolutely another as the format of successful response.So I see no reason the has all
SUCCESS,DATAandERRORSblocks. BooleanSUCCESSwill be clear from the context: it’s equal tofalseinside ofloadErrorand it’s totrueinside ofloadComplete.RESPONSE.DATAcan be directly the body of the response. and theRESPONSE.ERRORScan be the body of the error response.It’s the reason why both
loadErrorandloadCompletecallbacks exists.The
beforeProcessingwill be called only in case of successful response. The statementconsole.log(data);will don’t display the needed information becausedatawill be object and the text likeLOG: [object Object] beforeProcessing was calledwhich will be produced byconsole.log(data + ' beforeProcessing was called');get not much information.UPDATED: I don’t know ColdFusion and it’s restriction in the error handling. If you really can only to return in success case the data like
and in the case of an error
then you can do the following.
First of all you should define jsonReader option of jqGrid which corresponds to successful data:
To be able to read the error response you can place empty
"DATA"part in the response inside ofbeforeProcessing:In the real world you should display the error messages from
data.ERRORSin some div or dialog instead of usage ofalert, but the general schema described above should work.