简体   繁体   English

Axios HTTP 请求不发送到 Fastify 后端

[英]Axios HTTP requests not sending to Fastify backend

I'm trying to send a simple get request to my fastify server with axios, but i keep getting this error in the terminal:我正在尝试使用 axios 向我的 fastify 服务器发送一个简单的 get 请求,但我一直在终端中收到此错误:

Network Error
at node_modules/axios/lib/core/createError.js:15:17 in createError
at node_modules/axios/lib/adapters/xhr.js:114:22 in handleError
at node_modules/react-native/Libraries/Network/XMLHttpRequest.js:600:10 in setReadyState
at node_modules/react-native/Libraries/Network/XMLHttpRequest.js:395:6 in __didCompleteResponse
at node_modules/react-native/Libraries/vendor/emitter/EventEmitter.js:189:10 in emit

The request works perfectly fine in postman though, so I thought it was cors related and installed fastify-cors but I'm still getting the same error.不过,该请求在邮递员中运行良好,所以我认为它与 cors 相关并安装了 fastify-cors,但我仍然遇到相同的错误。

Does anyone have any suggestions how to fix this?有没有人有任何建议如何解决这个问题?

You have to explicitly fetch from your ip address, not your localhost.您必须明确地从您的 IP 地址中获取,而不是您的本地主机。 I fixed this by doing axios.get('http://<myIPaddr>:<port>') ,我通过执行axios.get('http://<myIPaddr>:<port>')

I was also told on the Fastify github help page to explicitly have the Fastify server listen on host '0.0.0.0'我还在 Fastify github 帮助页面上被告知明确让 Fastify 服务器在主机 '0.0.0.0' 上监听

fastify.listen({ port: 5000, host: "0.0.0.0" })

Now everything works fine.现在一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM