简体   繁体   English

如何在本地主机中获取 api,即 react js 中的 localhost/9563

[英]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 件事:

  1. CORS is enabled in backend CORS 在后端启用
  2. CORS Allow Credentials header is set in backend CORS 允许凭证 header 在后端设置
  3. in the frontend fetch is called using {withCredentials: true} option使用{withCredentials: true}选项调用前端提取
  4. Both backend and frontend run under the same port (especially important on localhost), so you may need nginx for proxying to backend/frontend in that case.后端和前端都在同一端口下运行(在 localhost 上尤其重要),因此在这种情况下,您可能需要 nginx 来代理后端/前端。

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

相关问题 如何从我的本地主机获取? 将 React.js 连接到 Node.js - How to Fetch from my local host? Connect React.js to Node.js 如何在获取 API React 时更改本地地址 - How to change local address in fetch API React event-stream.js我如何引用每行IE(0),(1)等 - event-stream.js how can i refrence each line I.E (0) , (1) ect ect 如何访问我在 react.j 的本地主机 node.js 中创建的 api - How to access the api I created in localhost node.js in react.j 如何在没有命令promt的情况下使用主管运行Node js应用程序(即我想在Eclipse中运行) - How to run node js application using supervisor without command promt(i.e I want run in eclipse) 使用 RabbitMQ 时如何同步 Node.js 和 Python 即在工作人员上运行任务,等待结果并将其发送给客户端 - How to synchronize Node.js and Python when using RabbitMQ i.e run a task on a worker, wait for the result and send it to the client React JS 中的异步 API 获取 - Async API Fetch in React JS 如何在 FETCH PUT API 中将 json 主体作为字符串传递给 React js - how to pass json body as String in FETCH PUT API in react js 如何在 React JS 中获取 API 并将数据保存到 MongoDB - How to fetch API in React JS and save the data to MongoDB 无法清除 npx 缓存并安装新版本的 create-react-app 即 5.0.0 - Not able to clear npx cache and install the new version of create-react-app i.e 5.0.0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM