angularjs – 离子应用程序无法连接启用服务器的$http

前端之家收集整理的这篇文章主要介绍了angularjs – 离子应用程序无法连接启用服务器的$http前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试用离子框架构建一个移动应用程序.当我的应用程序尝试连接服务器以获取json(服务器是web api和cors启用)它只返回404在genymotion和真实设备上.但是当我在浏览器中运行应用程序时,离子服务一切正常.

我很确定CORS是有效的.在应用程序在浏览器中工作时,我得到了响应头.

响应

  1. Access-Control-Allow-Origin:*
  2. Cache-Control:no-cache
  3. Content-Length:395
  4. Content-Type:application/json; charset=utf-8
  5. Date:Fri,08 May 2015 20:24:04 GMT
  6. Expires:-1
  7. Pragma:no-cache
  8. Server:Microsoft-IIS/7.0
  9. X-AspNet-Version:4.0.30319
  10. X-Powered-By:ASP.NET

要求:

  1. Accept:application/json,text/plain,*/*
  2. Accept-Encoding:gzip,deflate,lzma,sdch
  3. Accept-Language:tr-TR,tr;q=0.8,en-US;q=0.6,en;q=0.4
  4. Cache-Control:no-cache
  5. Connection:keep-alive
  6. DNT:1
  7. Host:*******:14400
  8. Origin:http://192.168.0.28:8100
  9. Pragma:no-cache
  10. Referer:http://192.168.0.28:8100/

Config.xml具有< access origin =“*”/>这条线在配置

在我的app.js中,我删除了所有http呼叫的X-Requested-With标头.

  1. .config(['$httpProvider',function($httpProvider) {
  2.  
  3. $httpProvider.defaults.useXDomain = true;
  4. delete $httpProvider.defaults.headers.common['X-Requested-With'];
  5. }
  6. ])

我简单地在我的工厂类中使用服务器的请求.

  1. $http.get(serverPath + "/api/mobilerest/mainPage");

当我在Genymode或真实设备中运行应用程序时,响应为404,并且statusText为“未找到”.我很确定web api是正在工作的,这个行为的原因是在离子型的应用程序,我的应用程序是本地文件和协议是file:///所以Origin标头是空的请求,然后服务器返回404.我也试过没有任何服务器的本地文件我得到与应用程序相同的错误.

  1. Accept:*/*
  2. Accept-Encoding:gzip,en;q=0.4
  3. Cache-Control:no-cache
  4. Connection:keep-alive
  5. DNT:1
  6. Host:server:14400
  7. Origin:null
  8. Pragma:no-cache

我错过了什么吗?

目前,cordova-plugin-whitelist似乎是“强制性”的.

安装它:

  1. cordova plugin add cordova-plugin-whitelist

配置config.xml

您可以使用*或更改限制性规则来保持当前设置

添加一个html策略
在index.html上,您还应该添加一个策略.
要授权一切,这里是:

  1. <Meta http-equiv="Content-Security-Policy" content="default-src *;
  2. style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline'
  3. 'unsafe-eval'"

猜你在找的Angularjs相关文章