error handling with .post()

前端 未结 3 858
囚心锁ツ
囚心锁ツ 2021-01-31 13:50

I have to modify a project written by someone else. Because the code is a mess I can\'t really change this $.post() (or replace it by $.ajax()). What I need to do, is to know if

3条回答
  •  时光取名叫无心
    2021-01-31 14:42

    There's a couple of ways to do it, but if you're not getting the error response available through the returned JSON data object, you can use $.ajaxSetup() to define defaults for all ajax calls through jQuery. Even though the $.post() method doesn't specify an error handler or callback in and of itself, the $.ajaxSetup() method will capture any ajax faults and execute the function you define for it.

    Alternatively, there is also an $.ajaxError() function that will do the same sort of thing.

    Note that this will not work if your JSON response is returned correctly but with a server-side error code. If you want to capture these sorts of cases, you might be best to look for them using the $.ajaxComplete() method

提交回复
热议问题