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

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

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

响应

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

要求:

@H_403_5@Accept:application/json,text/plain,*/* Accept-Encoding:gzip,deflate,lzma,sdch Accept-Language:tr-TR,tr;q=0.8,en-US;q=0.6,en;q=0.4 Cache-Control:no-cache Connection:keep-alive DNT:1 Host:*******:14400 Origin:http://192.168.0.28:8100 Pragma:no-cache Referer:http://192.168.0.28:8100/

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

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

@H_403_5@.config(['$httpProvider',function($httpProvider) { $httpProvider.defaults.useXDomain = true; delete $httpProvider.defaults.headers.common['X-Requested-With']; } ])

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

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

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

@H_403_5@Accept:*/* Accept-Encoding:gzip,en;q=0.4 Cache-Control:no-cache Connection:keep-alive DNT:1 Host:server:14400 Origin:null Pragma:no-cache

我错过了什么吗?

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

安装它:

@H_403_5@cordova plugin add cordova-plugin-whitelist

配置config.xml

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

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

@H_403_5@<Meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'"

猜你在找的Angularjs相关文章