jquery: is there a fail handler for $.post in Jquery?

混江龙づ霸主 提交于 2020-01-02 05:32:08

问题


When $.post succeeds, there is a success handler for it. What happens if it fails? Is there a similar handler that we can use for this case, so that we can inform the user that something is not happening right?


回答1:


There is not, according to the documentation, a specific error handler for $.post method.

What you have to do, if you want to have both the success and fail handlers, is to use the low-level $.ajax method. It's documentation can be found here: http://api.jquery.com/jQuery.ajax/

$.ajax({
  type: "POST",
  url: "some.php",
  success: function(html){
    /* Do success stuff here */
  },
  error: function(){
    /* do error stuff here */
  }
});



回答2:


You can catch it using .ajaxError(), but this applies to all ajax requests in your application. You also need to make sure you send back a HTTP error status back to the front end to be captured by jQuery.



来源:https://stackoverflow.com/questions/2846618/jquery-is-there-a-fail-handler-for-post-in-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!