A
A
Arthur2019-10-25 17:38:29
React
Arthur, 2019-10-25 17:38:29

How to make a 302 redirect fetch?

There are 3 pages in the react application
1- login (authorization form)
2- confrim (confirmation of sms)
3- some page from the
back when the login form is sent, either json is returned from the back, which says that you need to enter sms and ui shows the confirm page .
If SMS confirmation is disabled, a 302 redirect is returned from the back, by which you need to send the user to the page from this redirect.
The problem is that all requests go through fetch and in it I need to somehow get the url on which to send with a 302 redirect. How to get through to him?

const response = fetch(url, {
  method: "POST",
  redirect: 'manual',
  headers: {
    Accept: "applicaton/json",
  },
  dataType: "JSON",
  body: data,
})
.then(res => {
  console.log(res.url)
  document.location = res.url;
})

Here https://learn.javascript.ru/fetch-api#redirect
it is written that with the redirect flag: 'manual', in the res.url of the fetch, there will be a url from a 302 redirect. But this is not the case, the url will contain the url from which the request left.
In general, how to intercept the url from 302 and throw the user on it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Arthur, 2019-10-28
@cloudz

Finally did this:

const response = fetch(url, {
  method: "POST",
  headers: {
    Accept: "applicaton/json",
  },
  dataType: "JSON",
  body: data,
})
.then(res => {
  if (res.redirected) {
      document.location = res.url;
  }
})

N
nllsdfx, 2020-11-23
@nllsdfx

Redirected is an experimental feature, not supported by Safari.
https://developer.mozilla.org/en-US/docs/Web/API/R...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question