axios 和 android 模拟器的网络错误 [英] Network error with axios and android emulator

查看:66
本文介绍了axios 和 android 模拟器的网络错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 React-Native 应用程序,它与一个提供 API 的 NodeJS 后端一起工作.

I got a React-Native application working with a NodeJS backend which serves an API.

我的 React-Native 前端使用 Expo 和 Axios 访问我的 NodeJS API(使用 Hapi、Joi、Knex),这将(例如)更新我的数据库(MySQL).

My React-Native front is using Expo and Axios to hit on a route of my NodeJS API (using Hapi, Joi, Knex), which will (for the example) update my DB (MySQL).

我的 iOS 模拟器一切正常.但是,在 Android Emulator 上,我在路线上的一些点击不起作用"并显示以下错误消息:Network Error - node_modules/axios/lib/core/createError.js:16:24 in createError(其实是有效的,只是前端没有检测到...)

Everything works properly with my iOS simulator. However, on Android Emulator, SOME of my hits on route ""does not work"" with the following error message : Network Error - node_modules/axios/lib/core/createError.js:16:24 in createError (actually, it worked, but the front does not detect it...)

这很奇怪,因为就像我说的,这仅适用于我的某些路线.我将 http://localhost:8000/api 更改为 http://10.0.2.2:8000/api 以确保 Android Emulator 访问我的 API,并且它这部分没问题.

This is strange, because like I said, this is ONLY for some of my route. I changed the http://localhost:8000/api to http://10.0.2.2:8000/api to be sure that Android Emulator access my API, and it is ok for this part.

有问题的路由在 iOS 上正常工作,并且在 Insomnia/Postman ( localhost:8000/api/sendMail ) 上正常工作.它正在 Android Emulator 上运行,但应用程序没有检测到它.

The buggy route is working properly on iOS, and is working properly on Insomnia / Postman ( localhost:8000/api/sendMail ). It is working on Android Emulator, but the application does not detect it.

这是我的代码示例:

正面——按下我的发送电子邮件"按钮:

/* Button.js */
const sendAndEmailPromise = await sendEmail(this.state.email);

console.log(sendAndEmailPromise); // Output : NOTHING (not undefined, not null, N O T H I N G).

if (sendAndEmailPromise.status === 200) {
    // handle it
} else (sendAndEmailPromise.status === 403) {
    // etc. for each of my codeStatus that can be returned by my API 
}

/* MyAPI.js */
export function sendEmail(mail) {
    return axiosInstance
    .post(`/sendEmail`, null, {
      params: {
        mail
      },
    })
    .then(response => response) // Will not enter here
    .catch(error => {
       console.log(error); // Will output : NETWORK ERROR
    });
}

BACK -- 这是 sendEmail 承诺:

// Will return true of false :
const isMailSend = await sendTheEmail(mail);
console.log(isMailSend); // Output : TRUE and I receive the email on my gmail, so Android Emulator HIT the route.

if (isMailSend) {
  return handler
    .response({
      data: {
        message: "Email sent.",
      },
    })
    .code(200);
} else {
  // Handle it and return another response
}

我希望我的前台现在一切正常(实际上发生了......)并获得代码状态,而不是网络错误".

I expect my front to now that everything works fine (which actually happened...) and get the code status of that, instead of a "Network error".

更多,Axios 是否有可能获得另一个级别的错误?更具体的东西.

More, is it possible with Axios to get another level of error ? Something more specific.

一个 GitHub 问题,它不完全相同,但似乎与等效的东西相关:https://github.com/axios/axios/issues/973

A GitHub issue which is not totally the same but seems to relate something equivalent : https://github.com/axios/axios/issues/973

谢谢.

推荐答案

您在这里处理两个问题:

You are dealing with two problems here:

1) 您的模拟器无法工作,因为它无法将localhost"一词解析为 IP.就像使用 localhost2.com 一样,它不会解析某些内容.这个必须指向你服务器的本地ip 192.x.x.x

1) your emulator doesnt work because it cannot resolve the word "localhost" to an ip. is like using localhost2.com it will not resolve to something. This must be pointed to the local ip of your server 192.x.x.x

2) 您的服务器必须绑定到 0.0.0.0,因为通过将其绑定到 localhost,它无法解析为 192.x.x.x 等本地请求.如果您修复绑定,您的模拟器将有可能看到服务器,邮递员也会看到.

2) you server must be binded to 0.0.0.0 because by binding it to localhost it cannot resolve to local requests like 192.x.x.x. If you fix the binding your emulator would have the possibility to see the server and postman will too.

这篇关于axios 和 android 模拟器的网络错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆