离子应用程序无法使用的$ HTTP连接CORS启用服务器 [英] ionic app cannot connect cors enabled server with $http

查看:363
本文介绍了离子应用程序无法使用的$ HTTP连接CORS启用服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立与离子框架移动应用。当我的应用程序试图连接服务器以获取JSON(服务器的Web API和CORS启用),它只是返回404 genymotion和真实的设备。但是当我运行浏览器应用离子服务一切工作正常。

I am trying to build a mobile app with ionic framework. When my application tries to connect server to get json ( server is web api and cors is enabled) it just returns 404 on genymotion and real device. But when I run application in browser with ionic serve everything work fine.

我是pretty确保CORS是功能性的。在这里,响应头,而我在申请工作的浏览器了。

I am pretty sure CORS is functional. Here response header I got while application working in browser.

响应

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

请求

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中有<获得原产=*/> 此行的配置。

在我的app.js我删除的X要求,随着头部的所有HTTP调用。

in my app.js I removed X-Requested-With headers for all http calls.

.config(['$httpProvider', function($httpProvider) {

        $httpProvider.defaults.useXDomain = true;
        delete $httpProvider.defaults.headers.common['X-Requested-With'];  
    }
])

我简单的用get请求到服务器在我的工厂类。

I simple use get requests to server in my factory classes.

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

当我运行Genymode或实际设备应用程序,响应是404和状态文本是未找到。我是pretty确保网页API工作,对于这种行为的原因是离子型的应用程序,我的应用程序是本地文件和协议文件:///所以Origin标头为null,则在请求,则服务器返回404。我还尝试了本地文件没有我得到同样的错误,如应用程序中的任何服务器。

When I run application in Genymode or real device, response is 404 and statusText is 'not found'. I am pretty sure web api is working, the cause for this behaviour is in ionic based apps, my app is local file and protocol is file:/// so Origin header is null the in request, then server returns 404. I also tried a local file without any server I get the same error like in application.

Accept:*/*
Accept-Encoding:gzip, deflate, 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:server:14400
Origin:null
Pragma:no-cache

我缺少的东西吗?

Am I missing something ?

推荐答案

科尔多瓦 - 插件白名单似乎处于present强制。

cordova-plugin-whitelist seems to be "mandatory" at present.

安装:

cordova plugin add cordova-plugin-whitelist

配置config.xml中

您可以保留当前的设置与*或更改更严格的规则。

You can keep your current setup with * or change for more restrictive rules

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

add a html policy on index.html, you shall add a Policy also. To authorise everything, here it is :

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

这篇关于离子应用程序无法使用的$ HTTP连接CORS启用服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆