Source:  Twitter logo

I'm new in react . Trying to make "get" request and getting 403 error forbidden, "Response for preflight does not have HTTP ok status.". In network tab in Request Method instead of "get" method shows "options". What could be the problem? Cors already open , problem with token

let token = localStorage.getItem("token")
axios
  .get("http://dev.*****************get-template", {
    headers: {
      Authorization: `Bearer + ${token}`,
    },
  })
  .then(res => {
      console.log("Success")
  })
  .catch(error => {
    console.log(error)
  })

that's how I'm saving token. May be I'm not correctly saving it in localStorage? But when console.log it displays fine

 event.preventDefault()
    const formdata = new FormData()
    formdata.append("username", this.state.userLogin.email)
    formdata.append("password", this.state.userLogin.password)
    axios
      .post("http://dev.****************/get-token", formdata)
      .then(res => {
        if (res.data) {
          console.log(res.data)

          localStorage.setItem("token", res.data.access_token)
          localStorage.setItem("updToken", res.data.update_token)
          this.props.history.push("/settings")
        }
      })
      .catch(error => {
        console.log(error)
      })

I see a problem in your Bearer token

you write it:

 Authorization: `Bearer + ${token}`

but it should be :

Authorization: `Bearer ${token}`,

and the full answer is :

let token = localStorage.getItem("token")

axios
  .get("http://dev.*****************get-template", {
    headers: {
      Authorization: `Bearer ${token}`, //here remove + in template litereal
    },
  })
  .then(res => {
      console.log("Success")
  })
  .catch(error => {
    console.log(error)
  })
6 users liked answer #0dislike answer #06
MBehtemam profile pic
MBehtemam

Do it like this:

let token = localStorage.getItem("token")
axios.defaults.headers.common['Authorization'] = token

axios
  .get("http://dev.*****************get-template")
  .then(res => {
      console.log("Success")
  })
  .catch(error => {
    console.log(error)
  })
2 users liked answer #1dislike answer #12
Anurag Srivastava profile pic
Anurag Srivastava

This is due to CORS issue.

To solve this you need to set Access-Control-Allow-Origin header on your server side, allowing the domain from which you are sending the request or you can set it to *

0 users liked answer #2dislike answer #20
Thanthu profile pic
Thanthu

Copyright © 2022 QueryThreads

All content on Query Threads is licensed under the Creative Commons Attribution-ShareAlike 3.0 license (CC BY-SA 3.0).