How to prevent jQuery ajax from following a redirect after a post?

前端 未结 1 2055
后悔当初
后悔当初 2020-12-10 03:03

I have a link which dispatches an Ajax POST to perform an action. If the user is not yet signed in, the action method returns a 302 redirect to the login page. If this happe

相关标签:
1条回答
  • 2020-12-10 03:36

    This is not a limitation with jQuery but with JavaScript itself; there is no way to detect a redirect in an AJAX request in JavaScript; the browser XHR objects auto-magically and invisibly follow redirects as per the spec.

    The ideal option would be to add a catch in your server code to detect an AJAX request ("HTTP_X_REQUESTED_WITH" header), and return a special response to detect an anonymous user in your JavaScript, and handle appropriately.

    0 讨论(0)
提交回复
热议问题