2
   signIn = () => { 
    //post data to express backend
    fetch('http://......./api/v1/auth', {
        method: 'POST',
        header: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
              body: `login=${this.state.login}&password=${this.state.password}`
    })

    .then((response) => response.json())
    .then((res) => {

      if (res.success === true) {
          this.props.navigation.navigate('Authorized')
      } else {
          alert(res)
      }
    })
    .done();
}

I am continuously receiving such error and also network failed error when using localhost:3000

2
  • 2
    Most likely because the response is in html instead of JSON. Could you inspect the error code you got? Commented Dec 30, 2019 at 2:22
  • Print response and check what is content type... Commented Dec 30, 2019 at 4:28

1 Answer 1

2

Add try catch. Seems the response is in text/html format.

signIn = () => { 
//post data to express backend
fetch('http://......./api/v1/auth', {
    method: 'POST',
    header: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
    },
          body: `login=${this.state.login}&password=${this.state.password}`
})

.then(async (response) => {
  let res;
  try {
    res = await response.clone().json();
  } catch (e) {
    res = await response.text();
  }
  return res;
})
.then((res) => {

  if (res.success === true) {
      this.props.navigation.navigate('Authorized')
  } else {
      alert(res)
  }
})
.done();

}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.