网络错误 - React Native iOS 模拟器

Rya*_*zzo 2 https ios ios-simulator react-native axios

Network Error我有一个 React Native 应用程序,在尝试与后端 api 通信时抛出一个异常。

我尝试过的故障排除:

  • 相同的负载在Postman中工作(所以问题是模拟器/应用程序,而不是 api)
  • 使用Axios(通过await axios(config)
  • 尝试使用本地主机(http)和生产(https)服务器作为后端,同样的错误
  • 启用所有应用程序传输安全设置配置(允许任意加载,添加例外域)
  • 内容类型在标题中指定
  • 模拟器已连接到互联网(我尝试了浏览器)

配置:

 {"data": null, "headers": {"Content-Type": "application/json", "X-JWT": "..."}, "method": "GET", "url": "https://example.com/..."}
Run Code Online (Sandbox Code Playgroud)

错误:

{"message":"Network Error",
 "name":"Error",
 "stack":"createError@http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=com.exampleapp:156907:26\nhandleError@http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=com.exampleapp:156693:69\ndispatchEvent@http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=com.exampleapp:30339:31\nsetReadyState@http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=com.exampleapp:29485:33\n__didCompleteResponse@http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=com.exampleapp:29291:29\nemit@http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=com.exampleapp:2264:42\n__callFunction@http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=com.exampleapp:3086:36\n@http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=com.exampleapp:2810:31\n__guard@http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=com.exampleapp:3037:15\ncallFunctionReturnFlushedQueue@http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=com.exampleapp:2809:21\ncallFunctionReturnFlushedQueue@[native code]",
 "config":{
    "transitional":{"silentJSONParsing":true,"forcedJSONParsing":true,"clarifyTimeoutError":false},"transformRequest":[null],"transformResponse":[null],"timeout":0,"xsrfCookieName":"XSRF-TOKEN","xsrfHeaderName":"X-XSRF-TOKEN","maxContentLength":-1,"maxBodyLength":-1,
    "headers":{"Accept":"application/json, text/plain, */*","Content-Type":"application/json","X-JWT":"..."},
    "url":"https://example.com/...",
    "method":"get",
    "data":"null"
  },
 "status":null}
Run Code Online (Sandbox Code Playgroud)

技术环境:

  • 反应本机:0.64.1
  • 模拟器:iOS 15.2 iPhone 13
  • 设备:macOS Monterey (12.0.1)、Apple M1

Flo*_*bre 5

我使用 RN 也遇到同样的问题0.64.2

该问题似乎与 Chrome 调试器有关。

要修复网络错误,我只需禁用调试器或使用react-native-debugger

https://github.com/jhen0409/react-native-debugger

如果仍然不起作用,请尝试设置data: undefined请求GET

data: undefined修复和其他可能修复 的来源: https://github.com/axios/axios/issues/3192

更新:对于较新的 RN 版本,我使用 Flipper:RN 0.66、Flipper App 0.99 和 Flipper 依赖项 0.99。Flipper App 0.149+ 也可以工作,但在 Android 上网络扩展有时似乎工作得不太好。

我注意到,在我的具体 RN 版本中,使用 React Native 升级助手中推荐的 Flipper 版本对于 Android 和 iOS 来说效果最好。