【问题标题】:Prevent axios/fetch redirection防止 axios/fetch 重定向
【发布时间】:2018-01-27 03:37:06
【问题描述】:

我的应用程序有一个小问题: 我必须在一个 url 的响应中得到一些 cookie,其中一个是通过向另一个 url 发出请求来获得的。请求已完成,但我无法获取 cookie,因为当我获得请求的结果时,我会被重定向到站点/站点。 所以我的问题是:有没有办法授权第一次重定向,而不是第二次?

我知道我正在使用 react-native,而 axios 解决方案 maxRedirects: 1 在我的情况下不起作用。 fetch 中的redirect: 'manual' 也是如此。

请求代码:

fetch(
                    'https://gitlab.blablabla.fr/users/auth/ldapmain/callback' ,
                    {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/x-www-form-urlencoded',
                            'accept-charset':'utf-8',
                        },
                        body: querystring.stringify({
                            authenticity_token: this.state.token,
                            username: this.state.username,
                            password: this.state.password,
                        }),
                        redirect: 'manual',
                        credentials: 'include',
                        withCredentials: 'true',

                    }
                ).then( (responseData2) => {
                        console.log(this.state.username +"/"+ this.state.password+ "/"+this.state.token + "/"+ this.state.utf8);
                        console.log(responseData2);


                    })

以及显示的响应:

Response {
  type: 'default',
  status: 200,
  ok: true,
  statusText: undefined,
  headers: 
   Headers {
     map: 
      { 'x-version-id': [Object],
        server: [Object],
        'cache-control': [Object],
        date: [Object],
        'content-type': [Object],
        'content-length': [Object],
        'content-security-policy': [Object],
        'accept-ranges': [Object],
        'x-request-id': [Object],
        'last-modified': [Object],
        'x-frame-options': [Object] } },
  url: 'https://mattermost.blablabla.fr/',
  _bodyInit: '<!DOCTYPE html> <html> the html code</html> ',
  _bodyText: '<!DOCTYPE html> <html> the html code</html> ' }

【问题讨论】:

  • 您找到解决方案了吗?有一个非常相似的问题。
  • 嗨,我没有找到任何解决方案,但我通过使用 Cookies 代替它。 :/希望它会有所帮助

标签: javascript react-native fetch axios


【解决方案1】:

这是我的代码,我可以解决,防止获取重定向。

import 'whatwg-fetch';
fetch('/api/user/get_user_profile', 
    { 
        method: 'GET',
        headers: { 'Content-Type': 'application/json' },
        data: JSON.stringify({
            sHash: sHash
        }),
        redirect: 'manual',
    }
)

这是后端代码

res.status(200);
res.send(content);
return;

enter image description here

【讨论】:

  • HTTP 状态 200 不是“重定向”类型的一部分,3** 是。
猜你喜欢
  • 2018-09-14
  • 1970-01-01
  • 2012-03-29
  • 2012-12-10
  • 1970-01-01
  • 2010-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多