我在我的Reaction Native项目中使用了Fetch API.我向服务器发出HTTP POST请求,该服务器需要用户名和密码.如果登录成功,服务器在登录页面中设置会话cookie,然后重定向到另一个名为博客的网页.Fetch API收到来自博客的响应,该博客没有会话cookie.因此,我需要在重定向发生之前捕获重定向并存储会话cookie.

在文件中,它说redirect: 'manual'个人应该可以完成这项工作.然而,我还是从最后一页得到了回复.Fetch API-response.redirected返回undefined看不到重定向.但是请求和响应的URL是不同的.潜在的问题可能出在哪里?

const url = 'url of a website ( cannot show it to you )';
// login request
const login = (profileName, password) => {
  const details = {
    benutzername: profileName,
    passwort: password,
  };

  const formBody = Object.keys(details)
    .map((key) => `${encodeURIComponent(key)}=${encodeURIComponent(details[key])}`)
    .join('&');

  console.log(`formBody to be sent: ${formBody}`);

  fetch(url, {
    method: 'POST',
    headers: {
      Accept:
        'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
      'Content-Type': 'application/x-www-form-urlencoded',
    },
    body: formBody,
    redirect: 'manual',
  })
    .then((response) => {
      console.log('! response type !');
      console.log(response.type);
      console.log('! is redirected !');
      console.log(response.redirected);
      console.log('! response console log !');
      console.log(response.url);
      console.log('! response status console log !');
      console.log(response.status);
      return response.headers;
    })
    .then((headers) => {
      console.log('! headers for each !');
      headers.forEach((value, key) => {
        console.log(`${key} ==> ${value}`);
      });
      return headers;
    })
    .catch((error) => console.log(`error: ${error}`));
};

export { login };

以下是浏览器中的网络活动截图:

Login: Login page network activity

Blog: Blog page network activity

以下是日志(log):

LOG  ! response type !
 LOG  default
 LOG  ! is redirected !
 LOG  undefined
 LOG  ! response console log !
 LOG  https://.../Blog.xhtml?faces-redirect=true&dswid=-8890
 LOG  ! response status console log !
 LOG  200
 LOG  ! headers for each !
 LOG  cache-control ==> no-cache, no-store, must-revalidate
 LOG  content-encoding ==> gzip
 LOG  content-type ==> text/html;charset=UTF-8
 LOG  date ==> Sun, 23 Jul 2023 14:54:59 GMT
 LOG  expires ==> 0
 LOG  pragma ==> no-cache
 LOG  server ==> Apache
 LOG  set-cookie ==> dsrwid--8890=-8890; path=/; secure; Max-Age=60; Expires=Sun, 23-Jul-2023 14:55:59 GMT; SameSite=None
 LOG  vary ==> Accept-Encoding

推荐答案

目前,手动重定向不能与react native (see here)上的Fetch API一起使用

Javascript相关问答推荐

我无法使用tailwind-css和reactJS修改图像的位置

Vue-Router渲染组件但不更改网址

响应式JS DataTable中的Bootstrap 5弹出程序无法正常工作

如何在非独立组件中使用独立组件?

在NextJS中使用计时器循环逐个打开手风琴项目?

创建1:1比例元素,以另一个元素为中心

容器如何更改默认插槽中子项的显示?

如何将拖放功能添加到我已自定义为图像的文件输入HTML标签中?

为什么getRecord()会因为与_logger相关的错误而失败?(使用Hedera SDK)

传递一个大对象以在Express布局中呈现

如何从JSON数组添加Google Maps标记—或者如何为数组添加参数到标记构造器

使用下表中所示的值初始化一个二维数组

使用useEffect,axios和useParams进行react测试

ChartJs未呈现

如何使用JavaScript拆分带空格的单词

AJAX POST在控制器中返回空(ASP.NET MVC)

在表单集中保存更改时删除';禁用';

当从其他文件创建类实例时,为什么工作线程不工作?

有没有办法通过使用不同数组中的值进行排序

我为什么要使用回调而不是等待?