简体   繁体   English

React + Express RESTful API调用

[英]React + Express RESTful API calls

I'm trying to make a full-stack app (Express on the backend and React on the front) and I'm running into a problem. 我正在尝试制作一个全栈应用程序(后端为Express,前端为React),但遇到了问题。 The server and UI are both being tested in my DEV environment on localhost, but with different port numbers. 服务器和UI都在本地主机上的DEV环境中进行了测试,但是端口号不同。 Making API calls from the frontend to the backend are resulting in CORS errors, because they're both running on different ports. 从前端到后端进行API调用会导致CORS错误,因为它们都在不同的端口上运行。 I know a solution for this would be to implement server-side rendering, but due to a limitation of a 3rd party library that I'm using, I am unable to do this. 我知道解决方案是实现服务器端渲染,但是由于我使用的第三方库的限制,我无法做到这一点。

So I'm wondering what the best solution would be in this case. 所以我想知道在这种情况下最好的解决方案是什么。 Simply placing the ("Access-Control-Allow-Origin", "*") header fixes everything, but I'm not a fan of doing this. 只需放置(“ Access-Control-Allow-Origin”,“ *”)标头即可解决所有问题,但我不喜欢这样做。 Does anyone have any other suggestions? 还有其他建议吗?

Much appreciated--Thank you! 非常感谢-谢谢!

您需要启用CORS,或者可以使用“ Access-Control-Allow-Origin”,“ IP:PORT”指定地址。

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

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