【发布时间】:2015-07-19 21:42:01
【问题描述】:
我正在尝试使用 ionic 框架构建移动应用程序。当我的应用程序尝试连接服务器以获取 json(服务器是 web api 并且启用了 cors)时,它只会在 genymotion 和真实设备上返回 404。但是当我在浏览器中使用ionic serve 运行应用程序时,一切正常。
我很确定 CORS 可以正常工作。这是我在应用程序在浏览器中工作时得到的响应标头。
回应
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 在配置中有<access origin="*"/> 这一行
在我的 app.js 中,我删除了所有 http 调用的 X-Requested-With 标头。
.config(['$httpProvider', function($httpProvider) {
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
}
])
我在我的工厂类中简单地使用 get 请求到服务器。
$http.get(serverPath + "/api/mobilerest/mainPage");
当我在 Genymode 或真实设备中运行应用程序时,响应为 404 并且 statusText 为“未找到”。我很确定 web api 正在工作,这种行为的原因是在基于离子的应用程序中,我的应用程序是本地文件,协议是 file:/// 所以请求中的 Origin 标头为空,然后服务器返回 404。我也试过了没有任何服务器的本地文件我得到与应用程序相同的错误。
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
我错过了什么吗?
【问题讨论】:
标签: angularjs http cors ionic-framework asp.net-web-api2