Pablo
Pablo

Reputation: 167

jqGrid: displaying server error message

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" ?

Upvotes: 1

Views: 2343

Answers (2)

Hai Phan
Hai Phan

Reputation: 11

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

Upvotes: 1

Oleg
Oleg

Reputation: 221997

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.

Upvotes: 2

Related Questions