没有'Access-Control-Allow-Origin'Angular 4应用程序 [英] No 'Access-Control-Allow-Origin' Angular 4 app

查看:96
本文介绍了没有'Access-Control-Allow-Origin'Angular 4应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试访问此地址:
http://52.208.91.209: 3000 /?paging = 1



手动访问正常工作。
通过Angular 4请求访问返回:



请求的资源上没有Access-Control-Allow-Origin标头。因此,不允许访问 http:// localhost:4200



我用Google搜索了几个小时但没有找到解决方案。



我找到的唯一解决方案是使用 Allow-Control-Allow-Origin 插件。



我尝试访问的地址不是我的。



我只是想尝试使用它。



在使用带有角度CLI的ng服务时,我甚至读过关于代理的内容但并没有完全理解。



我看到的所有解决方案都只是服务器端的响应头问题。但是,这不是我的服务器所以我无法配置它。



任何帮助将不胜感激。谢谢。

解决方案

您可以更改前端JavaScript代码,而不是通过公共代理发出请求。



要尝试这样做,请更改您的代码以使用以下网址:

  https://cors-anywhere.herokuapp.com/http://52.208.91.209:3000/?paging=1 

这将导致请求转到 https://cors-anywhere.herokuapp.com ,这是一个发送请求的开放式CORS代理到你想要的 http://52.208.91.209:3000/?paging=1 网址。



该代理获取响应,获取响应并向其添加 Access-Control-Allow-Origin 响应标头,然后最终将其作为响应传递回您的请求前端代码。 / p>

所以最后因为浏览器看到带有 Access-Control-Allow-Origin 响应头的响应,浏览器允许您的前端JavaScript代码访问响应。



或者使用 https://github.com/Rob--W/cors-anywhere/ 或s uch设置你自己的代理。



在这种情况下你需要一个代理,因为 http://52.208.91.209:3000/?paging= 1 本身不会发送 Access-Control-Allow-Origin 响应标头 - 在这种情况下,您的浏览器将不允许您的前端JavaScript代码从该服务器跨域访问响应。



https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS 有更多详细信息。


I am trying to access this address: http://52.208.91.209:3000/?paging=1

Accessing manually works fine. Accessing via an Angular 4 request returns:

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.

I have googled it for a few hours and did not find a solution.

The only "solution" I have found is using Allow-Control-Allow-Origin plugin.

The address I am trying to access is not mine.

I am just trying to work with it.

I have even read about proxies when using ng serve with angular CLI but didnt fully understand.

All of the solutions I saw are simply a response headers problem on the server side. However, This is NOT my server so I cannot configure it.

Any help would be appreciated. Thank you.

解决方案

You can change your frontend JavaScript code to instead make the request through a public proxy.

To try that, change your code to use the following URL:

https://cors-anywhere.herokuapp.com/http://52.208.91.209:3000/?paging=1

That’ll cause the request to go to https://cors-anywhere.herokuapp.com, a open CORS proxy that sends the request on to the http://52.208.91.209:3000/?paging=1 URL you want.

That proxy gets the response, takes it and adds the Access-Control-Allow-Origin response header to it, and then finally passes that back to your requesting frontend code as the response.

So in the end because the browser sees a response with the Access-Control-Allow-Origin response header, the browser allows your frontend JavaScript code to access the response.

Or use the code from https://github.com/Rob--W/cors-anywhere/ or such to set up your own proxy.

You need a proxy in this case because http://52.208.91.209:3000/?paging=1 itself doesn’t send the Access-Control-Allow-Origin response header—and in that case your browser will not allow your frontend JavaScript code to access a response from that server cross-origin.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS has more details.

这篇关于没有'Access-Control-Allow-Origin'Angular 4应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆