简体   繁体   中英

POST request to receive redirect on client side

I have a service running that receives a POST request and returns a redirect. I have been able to test this using Postman and nghttp2.org (httpbin.org).

On postman, when I POST:

https://nghttp2.org/httpbin/redirect-to?url=https%3A%2F%2Fwww.google.com

The html received is google.com homepage

However to run this on a client side webpage I have the following code:

<html>
    <script type="text/javascript">

        function redirect() {
            fetch('https://nghttp2.org/httpbin/redirect-to?url=https%3A%2F%2Fwww.google.com', {
                headers: { 
                    'Content-Type': 'application/x-www-form-urlencoded',
                    'Accept': 'text/html',
                    'Access-Control-Allow-Origin': '*'
                },
                method: 'POST', 
                mode: 'no-cors',
                redirect: 'follow',
            })
        }

        redirect();

    </script>
</html>

I am running this webpage on a server, and it is not redirecting.

Is it possible to get the page to redirect automatically when this webpage is loaded? Or is there another approach I could make. I do need the request to be a POST and I do need the redirect to happen on client side due to cookie handling.

Cheers in advance!

Fetch doesn't directly redirect to another page requested by the server. But it can tell you that the server is requesting for redirect to another page and you can handle that accordingly. You can find more info on https://developer.mozilla.org/en-US/docs/Web/API/Response/redirected

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM