简体   繁体   English

前端 package.json 中的代理配置如何请求 void cors?

[英]How does a proxy config in frontend package.json a void cors request?

maybe I didn't quite understand proxys yet, but I have my server backend on localhost:7000 and my frontend client on localhost:3000.也许我还不太了解代理,但我的服务器后端位于 localhost:7000,前端客户端位于 localhost:3000。 Now I configured a proxy in my frontend package.json: "proxy": "http://127.0.0.1:7000" .现在我在我的前端 package.json: "proxy": "http://127.0.0.1:7000"中配置了一个代理。 This avoided the cors installation in the backend and accepted requests to my server.这避免了在后端安装 cors 并接受了对我的服务器的请求。 How does that work or why is it avoiding the cors configuration?这是如何工作的,或者为什么要避免 cors 配置? And whats the difference then between the proxy and cors?那么代理和cors有什么区别?

CORS errors occur up due to the same-origin policy that is implemented in most browsers. CORS 错误是由于大多数浏览器中实施的同源策略而发生的。 More specifically the reason you are getting the error is because you're making a request to a different origin from the one you're currently on.更具体地说,您收到错误的原因是因为您正在向与您当前所在的来源不同的来源发出请求。 In your case that's your frontend running on http://localhost:3000 which is trying to access the backend on http://localhost:7000在您的情况下,这是您在http://localhost:3000上运行的前端,它试图访问http://localhost:7000上的后端

In order to fix this, you could change your backend to send the Access-Control-Allow-Origin response header where you include the frontend's origin - or... you use a proxy.为了解决这个问题,您可以更改后端以发送Access-Control-Allow-Origin响应 header ,其中包含前端的来源 - 或者......您使用代理。

The Proxy basically attempts to fool the browser by pretending to have your backend on the same origin localhost:3000 whereas underneath the hood it's accessing the resource from the actual origin localhost:7000 .代理基本上试图通过假装让您的后端位于同一来源localhost:3000上来欺骗浏览器,而在后台它正在从实际来源localhost:7000访问资源。

You can read this article which explains it in further detail.你可以阅读这篇文章,它更详细地解释了它。

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

相关问题 如何仅在POST请求中在package.json中设置代理? - How to set up proxy in package.json for POST request only? package.json 中的代理不影响获取请求 - Proxy in package.json not affecting fetch request create-react-app 的 package.json 中的“代理”字段如何工作? - How does the “proxy” field in a create-react-app's package.json work? 添加代理以反应 package.json 后,不断收到“代理错误:无法代理请求”错误 - Keep getting “Proxy error: Could not proxy request” error after adding proxy to react package.json 如何访问package.json配置部分? - How I can access to package.json config section? 将 package.json 中的代理设置为 env 变量 - set proxy in package.json to env variable 如何在生产模式下使用类似 package.json 中的代理参数的 npm 代理? - How to use proxy in production mode with npm like proxy parameter in package.json? 如何使用 package.json 中定义的代理 url 作为 axios 代理? - How can i use proxy url defined in package.json as axios proxy? 前后端可以共享同一个package.json吗? - Can the frontend and backend share the same package.json? 不定义代理但得到“当在 package.json 中指定“代理”时”错误 - Define no proxy but get 'When “proxy” is specified in package.json' error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM