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_policyhttps://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
这篇关于IONIC, Access-Control-Allow-Origin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!