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 01 Answer
Reset to default 10The 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).
本文标签:
版权声明:本文标题:javascript - What are jQuery best practices regarding Ajax convenience methods and error handling? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744537844a2611422.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论