Get nice exception output from service for Ajax

April 1st, 2013

Debugging REST services called by AJAX can be tiresome, so here’s an easy way to get a nice formatted error output from the response.

?View Code JAVASCRIPT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$.ajax({
            url: "/MyService.rst/MyServiceMethod",
            contentType: "application/json",
            dataType: "json",
            data: JSON.stringify(request),
            type: "POST",
            success: function (data) {
                // request returned success
            },
            error: function (xhr, ajaxOptions, thrownError) {
                document.write("Error " + xhr.status + " - " + thrownError + "<br /><br />" + xhr.responseText.replace(/[\r\n]/g, '<br />'));
            },
            complete: function () {
            }
        });

Leave a Reply