IONIC,Access-Control-Allow-Origin [英] IONIC, Access-Control-Allow-Origin
问题描述
我尝试使用以下代码发送带有$ http(angular)的http请求:
I try to send http request with $http (angular) with this code:
$http({
method: 'GET',
url: 'http://192.168.0.17:9000',
header: {'Access-Control-Allow-Origin': "*"},
}).then(getEventsSuccess, getEventsError);
但这不起作用,我在网络控制台中出现此错误:
But this doesn't work and I have in the web console this error:
XMLHttpRequest cannot load http://192.168.0.17:9000/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access.
你有解决方案吗?
推荐答案
由于浏览器中实现的安全机制,您会看到此错误,称为同源策略。
You see this error due to a security mechanism implemented in your browser, called Same Origin Policy.
基本上,它是由于您的网页尝试访问位于与网页本身不同的主机,端口或方案(HTTP / HTTPS /文件等)上的服务器上的资源。
Basically, it is caused since your webpage tries to access a resource which resides on a server that is on a different Host, Port or Scheme (HTTP / HTTPS / file etc) than the webpage itself.
要解决此问题,您可以执行以下操作之一:
In order to solve this issue, you can do one of the following:
- 从您尝试访问的服务器提供网页。如果您的网页网址为 http://192.168.0.17:9000/X.html ,请求应该成功并且错误将消失。
- 为从服务器发送的响应添加一个特殊标头,称为Access-Control-Allow-Origin。
- Serve your Webpage from the server that you are trying to access. If your webpage URL will be http://192.168.0.17:9000/X.html, your request should be successful and the error will disappear.
- Add a special header to the response sent from your server, called Access-Control-Allow-Origin.
在此处阅读更多内容:
https://en.wikipedia.org/wiki/Same-origin_policy
https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
Read more here: https://en.wikipedia.org/wiki/Same-origin_policy https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
这篇关于IONIC,Access-Control-Allow-Origin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!