How can I get JQGrid to recognize server sent Errors?

后端 未结 8 1061
清酒与你
清酒与你 2020-12-09 16:42

I have a jqgrid that\'s functionning very well.

I was wondering is it possible to catch server sent errors ? how Is it done ?

8条回答
  •  感情败类
    2020-12-09 16:55

    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 method
    • getJsonError(data): returns the string with the error message as defined in your web method
    • ShowErrorDialog(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.

提交回复
热议问题