简体   繁体   English

在客户端接收重定向的 POST 请求

[英]POST request to receive redirect on client side

I have a service running that receives a POST request and returns a redirect.我有一个正在运行的服务,它接收一个 POST 请求并返回一个重定向。 I have been able to test this using Postman and nghttp2.org (httpbin.org).我已经能够使用 Postman 和 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收到的html是google.com主页

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.我确实需要将请求设为 POST,并且由于 cookie 处理,我确实需要在客户端进行重定向。

Cheers in advance!提前加油!

Fetch doesn't directly redirect to another page requested by the server. Fetch 不会直接重定向到服务器请求的另一个页面。 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您可以在https://developer.mozilla.org/en-US/docs/Web/API/Response/redirected上找到更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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