ionic 应用程序无法使用 $http 连接启用 cors 的服务器

编程入门 行业动态 更新时间:2024-10-26 01:30:25
本文介绍了ionic 应用程序无法使用 $http 连接启用 cors 的服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用离子框架构建一个移动应用程序.当我的应用程序尝试连接服务器以获取 json(服务器是 web api 并且启用了 cors)时,它只在 genymotion 和真实设备上返回 404.但是当我使用 ionic serve 在浏览器中运行应用程序时,一切正常.

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.

我很确定 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:192.168.0.28:8100 Pragma:no-cache Referer:192.168.0.28:8100/

Config.xml 在配置中有 这一行

Config.xml has <access origin="*"/> this line in configuration

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

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.headersmon['X-Requested-With']; } ])

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

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

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

当我在 Genymode 或真实设备中运行应用程序时,响应为 404 并且 statusText 为未找到".我很确定 web api 正在工作,这种行为的原因是基于 ionic 的应用程序,我的应用程序是本地文件,协议是 file:///所以 Origin 标头为空请求,然后服务器返回 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

我错过了什么吗?

推荐答案

cordova-plugin-whitelist 目前似乎是强制性的".

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'"

更多推荐

ionic 应用程序无法使用 $http 连接启用 cors 的服务器

本文发布于:2023-07-27 14:08:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1222773.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   服务器   ionic   cors   http

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!