AJAX redirect dilemma, how to get redirect URL OR how to set properties for redirect request

前端 未结 6 496
甜味超标
甜味超标 2021-02-01 05:47

First, I\'m working in Google Chrome, if that helps. Here is the behavior:

I send an xhr request via jQuery to a remote site (this is a chrome Extension, and I\'ve set a

6条回答
  •  被撕碎了的回忆
    2021-02-01 06:17

    You could try the type of solution described here: How to manage a redirect request after a jQuery Ajax call

    That means in fact superseding HTTP protocol, you'll need to control the server as well. All ajax responses will be in code 200 (or at least the 3xx ones, not the 401/403) with a json object. and in this json object you can provide some special error code (why not reusing

    Then you extends jQuery.ajax function to capture theses special codes in your json response and make the new requests needed. In fact the automatic jQuery redirect call is done by you and not by jQuery.

    at first this seems ugly, but when dealing with problems like the one suggested in the link (end of session, redirect on login page) it seems that having a complete protocol handling in your ajax json communications between the client and the server is not a bad idea.

提交回复
热议问题