1

I have a jqGrid edit form that contains a date field. I want to implement exception handling so that error messages from the server will be displayed in the edit form. Example response:

HTTP/1.1 400 Bad Request
Server: Apache-Coyote/1.1
Date: Fri, 28 Jun 2013 15:47:21 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: close

11
"Bad Date format"
0

jqGrid only displays "error status:'Bad Request' Error Code: 400", at the top of the form. How can I have it also display the error message: "Bad Date format" ?

Pablo
  • 167
  • 1
  • 2
  • 13

2 Answers2

2

You should use errorTextFormat callback of form editing. As the parameter the callback get jqXHR object which is wrapper on XMLHTTPRequest object. It's responseText property represent the body of the response (11\n"Bad Date format"\n0 in your case). The status property gets you the HTTP status code (400 in your example). You can use getResponseHeader and getAllResponseHeaders to examine all HTTP headers.

By the way I find very strange that the response contains Content-Type: application/json, but the body of the response don't contains JSON string.

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Oleg, thank you for your response. I followed your instructions and was able to read the message error this way: {mtype:"POST", addCaption:"Add New System Message", errorTextFormat: function(request) { var message = request.responseText; alert(message); }}, ... Now how do I get the messag to actually display automatically at the top of the form, if possible appended to the rest of the error message? – Pablo Jun 29 '13 at 16:25
  • BTW the content-type is the originally declared mime-type, as returned by CXF. It doesn't update it in case of error apparently. – Pablo Jun 29 '13 at 16:28
  • @Pablo: `errorTextFormat` callback should return string with the text or HTML fragment which should be displayed. You can find an example in [the answer](http://stackoverflow.com/a/6803206/315935). See [the demo](http://www.ok-soft-gmbh.com/jqGrid/SimpleLocalGridWithMsgInEditForm.htm) from the answer. Be carefull, the part of the code of `errorTextFormat` in the demo is **dummy code** which should be removed. – Oleg Jun 29 '13 at 16:36
  • Got it Oleg. Thank you once again. – Pablo Jun 29 '13 at 17:49
1

Assume that your response contains 2 fields: status (OK, ERROR,..) and message then you should write a function like this:

    validateAfterSubmit = function(response, postdata){
        var json   = response.responseText; // response text is returned from server.
        var result = JSON.parse(json); // convert json object into javascript object.
        return [result.status == 'OK', result.message, null];
    };

and specify in the edit/add options:

            //edit options
            { url: '...',
                afterSubmit: validateAfterSubmit
            },
            //add options
            { url: '...',
                afterSubmit: validateAfterSubmit
            },

hopefully this can help

Hai Phan
  • 11
  • 3