IONIC,Access-Control-Allow-Origin

时间:2015-10-05 15:00:33

标签: angularjs ionic-framework ionic

我尝试使用以下代码发送带有$ http(angular)的http请求:

$http({
              method: 'GET',
              url: 'http://192.168.0.17:9000',
              header: {'Access-Control-Allow-Origin': "*"},
        }).then(getEventsSuccess, getEventsError);

但这不起作用,我在网络控制台中出现了这个错误:

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.

你有解决方案吗?

1 个答案:

答案 0 :(得分:4)

由于浏览器中实现了一种名为Same Origin Policy的安全机制,您会看到此错误。

基本上,这是因为您的网页试图访问位于与网页本身不同的主机,端口或方案(HTTP / HTTPS /文件等)上的服务器上的资源。

要解决此问题,您可以执行以下操作之一:

  • 从您尝试访问的服务器提供网页。如果您的网页网址为http://192.168.0.17:9000/X.html,则您的请求应该会成功,错误就会消失。
  • 为从服务器发送的响应添加一个特殊标头,称为Access-Control-Allow-Origin。

在这里阅读更多内容: https://en.wikipedia.org/wiki/Same-origin_policy https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

相关问题