Change page in the middle of Ajax request

前端 未结 5 616
面向向阳花
面向向阳花 2021-01-17 12:07

What happens if I send some ajax request, and immediately change the page (say follow some link) before the request returns? I mean I suppose the XHR object sends a request

5条回答
  •  佛祖请我去吃肉
    2021-01-17 12:31

    The behavior seems to be when the user leaves a page, any outstanding ajax requests complete with 0 as their status. jQuery interprets this as an error, which is why it calls the error handler. In your error handler, you can call the same function that your success handler calls if xhr.status == 0, otherwise execute the desired error behavior.

提交回复
热议问题