在获取帖子调用后重定向 [英] redirect after a fetch post call

查看:100
本文介绍了在获取帖子调用后重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用访问管理(AM)服务器创建一个社交登录页面. 当用户单击登录按钮时,我将对AM服务器进行一次http调用. AM服务器生成带有身份验证cookie的HTTP 301重定向响应到社交登录页面.我需要以某种方式遵循此重定向响应,并在网络浏览器中显示新内容.

I am creating an social login page with an Access Management (AM) server. When user click on the login button then I make a fetch http post call to AM server. AM server generates a HTTP 301 redirect response with auth cookies to the social login page. I need to follow somehow this redirect response and show the new content in the web browser.

UI:ReactJS

UI: ReactJS

请求:

POST /api/auth/socialauth/initiate HTTP/1.1
Host    example.com
User-Agent  Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:49.0)
Accept  */*
Accept-Language en-US,en;q=0.5
Accept-Encoding gzip, deflate
origin  http://web.example.com:8080
Referer http://web.example.com:8080/myapp/login
Cookie  authId=...; NTID=...

响应

HTTP/1.1 307 Temporary Redirect
https://www.facebook.com/dialog/oauth?client_id=...&scope=public_profile%2Cemail&redirect_uri=http%3A%2F%2Fam.example.com%3A8083%2Fopenam%2Foauth2c%2FOAuthProxy.jsp&response_type=code&state=qtrwtidnwdpbft4ctj2e9mv3mjkifqo

反应代码:

initiateSocialLogin() {
    var url = "/api/auth/socialauth/initiate";

    fetch(url, { method: 'POST' })
        .then(response => {
            // HTTP 301 response
            // HOW CAN I FOLLOW THE HTTP REDIRECT RESPONSE?
        })
        .catch(function(err) {
            console.info(err + " url: " + url);
        });
}

如何跟踪重定向响应并在网络浏览器中显示新内容?

How I can follow the redirect response and show the new content in the web browser?

推荐答案

"error""manual".

如果为"follow",则fetch()API遵循重定向响应(HTTP 状态代码= 301,302,303,307,308).

If it is "follow", fetch() API follows the redirect response (HTTP status code = 301,302,303,307,308).

如果是错误",则fetch()API会将重定向响应视为 错误.

If it is "error", fetch() API treats the redirect response as an error.

如果它是"manual",则fetch()API不遵循重定向并返回 不透明重定向过滤的响应,其中包含重定向 响应.

If it is "manual", fetch() API doesn't follow the redirect and returns an opaque-redirect filtered response which wraps the redirect response.

由于您要在提取后进行重定向,因此只需将其用作

Since you want to redirect after a fetch just use it as

fetch(url, { method: 'POST', redirect: 'follow'})
    .then(response => {
        // HTTP 301 response
    })
    .catch(function(err) {
        console.info(err + " url: " + url);
    });

这篇关于在获取帖子调用后重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆