admin管理员组

文章数量:1387460

Let's suppose, for an example, that I want to partly clone Gmail's interface with jQuery Ajax and implement periodic auto-saving as well as sending. And in particular, let us suppose that I care about error handling, expecting network and other errors, and instead of just being optimistic I want sensible handling of different errors.

If I use the "low-level" feature of $.ajax() then it's clear how to specify an error callback, but the convenience methods of $.get(), $.post(), and .load() do not allow an error callback to be specified.

What are the best practices for pessimistic error handling? Is it by registering a .ajaxError() with certain wrapped sets, or an introspection-style global error handler in $.ajaxSetup()? What would the relevant portions of code look like to initiate an autosave so that a "could not autosave" type warning is displayed if an attempted autosave fails, and perhaps a message that is customized to the type of error?

Thanks,

Let's suppose, for an example, that I want to partly clone Gmail's interface with jQuery Ajax and implement periodic auto-saving as well as sending. And in particular, let us suppose that I care about error handling, expecting network and other errors, and instead of just being optimistic I want sensible handling of different errors.

If I use the "low-level" feature of $.ajax() then it's clear how to specify an error callback, but the convenience methods of $.get(), $.post(), and .load() do not allow an error callback to be specified.

What are the best practices for pessimistic error handling? Is it by registering a .ajaxError() with certain wrapped sets, or an introspection-style global error handler in $.ajaxSetup()? What would the relevant portions of code look like to initiate an autosave so that a "could not autosave" type warning is displayed if an attempted autosave fails, and perhaps a message that is customized to the type of error?

Thanks,

Share Improve this question edited Jun 26, 2015 at 17:25 Sumurai8 20.8k11 gold badges69 silver badges102 bronze badges asked May 21, 2010 at 14:17 Christos HaywardChristos Hayward 5,99317 gold badges61 silver badges116 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 10

The mon practice is to use $.ajaxSetup to specify a generic callback handler for errors during the non-$.ajax functions. E.g.

function init() {
    $.ajaxSetup({
        error: handleXhrError
    });
}

function handleXhrError(xhr, errorType, exceptionThrown) {
    // ...
}

Inside the handleXhrError you can display either a modal window or some notification bar like Gmail does, or replace the entire document, depending on the functional requirements. You can take action based on the response body as obtained by xhr.responseText and/or the HTTP status code from xhr.status. The values of the response body and status are controllable from the server side on. Those should provide enough information about the problem and which error action to take. The errorType will equal to 'timeout' when a timeout has occurred (i.e. network problem).

本文标签: