jQuery and AJAX response header

前端 未结 8 2489
夕颜
夕颜 2020-11-22 10:45

So I\'ve got this jQuery AJAX call, and the response comes from the server in the form of a 302 redirect. I\'d like to take this redirect and load it in an iframe, but when

8条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 11:02

    The unfortunate truth about AJAX and the 302 redirect is that you can't get the headers from the return because the browser never gives them to the XHR. When a browser sees a 302 it automatically applies the redirect. In this case, you would see the header in firebug because the browser got it, but you would not see it in ajax, because the browser did not pass it. This is why the success and the error handlers never get called. Only the complete handler is called.

    http://www.checkupdown.com/status/E302.html

    The 302 response from the Web server should always include an alternative URL to which redirection should occur. If it does, a Web browser will immediately retry the alternative URL. So you never actually see a 302 error in a Web browser
    

    Here are some stackoverflow posts on the subject. Some of the posts describe hacks to get around this issue.

    How to manage a redirect request after a jQuery Ajax call

    Catching 302 FOUND in JavaScript

    HTTP redirect: 301 (permanent) vs. 302 (temporary)

提交回复
热议问题