LoginSignup
3
2

More than 1 year has passed since last update.

react nativeでapiたたいたら[AxiosError: Network Error]が出てきたので修正する

Posted at

修正前

try {
  console.log("try to create user");
  await axios({
    method: 'post',
    url: 'http://localhost:5050/api/v1/users',
    data: jsonData,
  }).then((response) => {
    console.log(response.data);
  }).catch(error => console.log(error));
} catch (ex: any) {
  console.error("ex", ex);
}

修正後

try {
  console.log("try to create user");
  await axios({
    method: 'post',
    url: 'http://10.0.2.2:5050/api/v1/users',
    data: jsonData,
  }).then((response) => {
    console.log(response.data);
  }).catch(error => console.log(error));
} catch (ex: any) {
  console.error("ex", ex);
}

解説編

👆こちらに以下のような表があります!

スクリーンショット 2022-10-25 23.16.18.png

エミュレーターからするとlocalhostとはエミュレーター自身のことで、普段のweb開発で使用するlocalhost(127.0.0.1)で使うような開発マシンは「10.0.2.2」となります!
これで解決できるわけですね!

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2