我正在使用访问管理 (AM) 服务器创建社交登录页面。 当用户单击登录按钮时,我会对 AM 服务器进行 fetch http post 调用。 AM 服务器生成带有身份验证 cookie 的 HTTP 301 重定向响应到社交登录页面。我需要以某种方式遵循此重定向响应并在网络浏览器中显示新内容。
用户界面: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
react 代码:
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);
});
}
如何跟踪重定向响应并在网络浏览器中显示新内容?
最佳答案
Request.redirect可以是“follow”
、“error”
或“manual”
。
If it is "follow", fetch() API follows the redirect response (HTTP status code = 301,302,303,307,308).
If it is "error", fetch() API treats the redirect response as an error.
If it is "manual", fetch() API doesn't follow the redirect and returns an opaque-redirect filtered response which wraps the redirect response.
由于您想在获取后重定向,只需将其用作
fetch(url, { method: 'POST', redirect: 'follow'})
.then(response => {
// HTTP 301 response
})
.catch(function(err) {
console.info(err + " url: " + url);
});
关于javascript - fetch post 调用后重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39735496/