jQuery and AJAX response header

前端 未结 8 2535
夕颜
夕颜 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 10:58

    The underlying XMLHttpRequest object used by jQuery will always silently follow redirects rather than return a 302 status code. Therefore, you can't use jQuery's AJAX request functionality to get the returned URL. Instead, you need to put all the data into a form and submit the form with the target attribute set to the value of the name attribute of the iframe:

    $('#myIframe').attr('name', 'myIframe');
    
    var form = $('
    ').attr('target', 'myIframe'); $('').attr({name: 'search', value: 'test'}).appendTo(form); form.appendTo(document.body); form.submit();

    The server's url.do page will be loaded in the iframe, but when its 302 status arrives, the iframe will be redirected to the final destination.

提交回复
热议问题