vijayst
vijayst

Reputation: 21846

Error while using fetch in react native app

I am using the following fetch call in a react native app:

fetch(apiUrls.signInUrl, {
  method: 'POST',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    user: {
      email: username,
      password: password,
    }
  })
})
.then(response => {
  response.json().then(responseJson => {
    dispatch(AccountActions.loginResponse(username, password, responseJson.token, response.status));
    dispatch(CardActions.getCards(username, responseJson.token));
  });
})
.catch(err => {
  console.error(err);
});

The fetch call is failing with the following error screen. Any help? enter image description here

Upvotes: 0

Views: 694

Answers (1)

Bipin Bhandari
Bipin Bhandari

Reputation: 2692

In IOS http connections are disabled by default. So you would have to enable it. Though it isn't recommended, it should be fine for debug mode.

Here are the steps: 1. You go to Info.plist file of your iOS project in Xcode

  1. Change allow Arbitrary loads to "YES" enter image description here

  2. Optionally you can also add your trusted host to exception domains if you don't like to enable "allow arbitrary loads" option

Good luck!

Upvotes: 1

Related Questions