防止axios / fetch重定向

时间:2017-08-18 12:36:26

标签: javascript react-native fetch axios

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

我预先确定我正在使用react-native,而axios解决方案(maxRedirects:1)在我的情况下不起作用。同样适用于"重定向:'手册'"在获取。

请求代码:

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> ' }

1 个答案:

答案 0 :(得分:0)

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

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

相关问题