[英]How to fetch api in local host i.e localhost/9563 in react js
When I build the my react js it's not showing any fetch api and gave me error (get/playlist) When I developed the react js web I give proxy in package.json file.I know that it's work only on development side but how to fetch api when I build the website and that present to client with fetch api can I use express js? When I build the my react js it's not showing any fetch api and gave me error (get/playlist) When I developed the react js web I give proxy in package.json file.I know that it's work only on development side but how to当我构建网站时获取 api 并通过 fetch api 呈现给客户端我可以使用 express js 吗?
Api that I fetch is in localhost/9563 React js run in localhost/3000我获取的 Api 在 localhost/9563 React js 在 localhost/3000 中运行
The easiest way is to use the proxy from react: https://create-react-app.dev/docs/proxying-api-requests-in-development/ - which you might already do based on your question.最简单的方法是使用 react 中的代理: https://create-react-app.dev/docs/proxying-api-requests-in-development/ - 根据您的问题,您可能已经这样做了。
Can you maybe share your package.json?你能分享你的 package.json 吗? I suspect you might have misconfigured it.
我怀疑你可能配置错误。
Alternatively if you do not wish to use the proxy:或者,如果您不想使用代理:
Make sure to enable CORS in your Express application, due to different ports between frontend and backend, the same-origin policy of your browser does not apply.确保在您的 Express 应用程序中启用 CORS,由于前端和后端之间的端口不同,您的浏览器的同源策略不适用。 So the request will not be run.
所以请求不会被运行。
You can use the CORS middleware for it: https://expressjs.com/en/resources/middleware/cors.html您可以使用 CORS 中间件: https://expressjs.com/en/resources/middleware/cors.html
Once your Express backend allows CORS, your request should run just fine.一旦您的 Express 后端允许 CORS,您的请求应该可以正常运行。
As a side note: Server-side cookies will NOT be sent this way, as many browsers do not send cookies if the port is different.附带说明:服务器端 cookies 不会以这种方式发送,因为如果端口不同,许多浏览器不会发送 cookies。 If you need server-side cookies to be sent along in your fetch request, you need to have 3 things in place:
如果您需要在您的 fetch 请求中发送服务器端 cookies,您需要准备好 3 件事:
{withCredentials: true}
option{withCredentials: true}
选项调用前端提取
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.