Catching 302 FOUND in JavaScript

后端 未结 5 2104
予麋鹿
予麋鹿 2020-11-27 03:32

I use jQuery to make an AJAX POST request to my server, which can return HTTP response with status 302. Then JavaScript just sends GET request to this URL, while I\'d like t

5条回答
  •  春和景丽
    2020-11-27 04:25

    I don't think so. The W3C says that HTTP redirects with certain status codes, including 302, must be transparently followed. Quoted below:

    If the response is an HTTP redirect (status code 301, 302, 303 or 307), then it MUST be transparently followed (unless it violates security or infinite loop precautions). Any other error (including a 401) MUST cause the object to use that error page as the response.

    As an experiment, I tried doing Ajax requests from various browsers (Firefox 3.5, Chrome, IE8, IE7, IE6) to a server giving a 302 status code, and showing the status in the browser's request object. In every case, it showed up as 200.

提交回复
热议问题