在我的项目中,我已在云服务器中部署了 Web API 并从该服务获取数据。在纹波模拟器中它工作得很好。但是当我在设备模式(Android Tab)下调试时,它可以调用该服务。它也没有显示任何特定的错误消息。
在我的index.html页面中
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<title></title>
<link href="lib/ionic/css/ionic.css" rel="stylesheet" />
<link href="css/style.css" rel="stylesheet">
<!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
<link href="css/ionic.app.css" rel="stylesheet">
-->
<!-- ionic/angularjs js -->
<script src="lib/ionic/js/ionic.bundle.js"></script>
<script src="lib/ionic/js/angular/angular-resource.min.js"></script>
<script src="lib/ng-cordova.min.js"></script>
<!-- cordova script (this will be a 404 during development) -->
<script src="cordova.js"></script>
<!--angularJS local storage-->
<script src="js/angular-messages.min.js"></script>
<script src="js/angular-local-storage.min.js"></script>
<!-- your app's js -->
<script src="app/app.js"></script>
<script src="app/services/authIntercepterService.js"></script>
<script src="app/services/productService.js"></script>
<script src="app/controllers/mainAppController.js"></script>
<script src="app/controllers/productsController.js"></script>
</head>
<body ng-app="app">
<ion-nav-view></ion-nav-view>
</body>
</html>
这是产品服务
app.factory('productService', ['$http', '$q', 'localStorageService', 'ngAuthSettings', function ($http, $q, localStorageService, ngAuthSettings) {
var serviceBase = ngAuthSettings.apiServiceBaseUri;
var productServiceFactory = {};
var _getAllDishCategories = function () {
var deferred = $q.defer();
$http({
method: 'GET',
url: serviceBase + "api/Product/GetAllDishCategories"
}).success(function (response) {
deferred.resolve(response);
}).error(function (err, status, header, config) {
deferred.reject(err);
});
return deferred.promise;
};
var _getProductById = function (productId) {
var deferred = $q.defer();
$http({
method: 'GET',
url: serviceBase + "api/Product/GetProductById",
params: {
productId: productId
}
}).success(function (response) {
deferred.resolve(response);
}).error(function (err, status, header, config) {
deferred.reject(err);
});
return deferred.promise;
};
productServiceFactory.getAllDishCategories = _getAllDishCategories;
productServiceFactory.getProductById = _getProductById;
return productServiceFactory;
}]);
这是我的 Web API Controller
using FandBClassLibrary;
using FandBViewModel.Product;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace FandBWebAPI.Controllers
{
[RoutePrefix("api/Product")]
public class ProductController : ApiController
{
#region Member variable
private IProductManager productManager;
#endregion
#region Constructor
public ProductController()
{
}
public ProductController(IProductManager productManager)
{
this.productManager = productManager;
}
#endregion
#region Post Methods
[HttpGet]
[Route("GetAllDishCategories")]
public List<DishCategoryViewModel> GetAllDishCategories()
{
var dcList= productManager.GetAllDishCategories();
return dcList;
}
[HttpGet]
[Route("GetProductById")]
public ProductViewModel GetProductById(string productId)
{
var productdetails = productManager.GetProductById(productId);
return productdetails;
}
#endregion
}
}
这里我在启动类中启用了CROS
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Owin;
using Owin;
[assembly: OwinStartup(typeof(FandBWebAPI.Startup))]
namespace FandBWebAPI
{
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
}
}
}
有人可以帮我解决这个问题吗?
谢谢, 埃兰迪卡
最佳答案
我必须安装白名单插件。之后它开始工作。您可以使用以下命令来安装插件。要在命令提示符下执行此操作,请转到项目文件夹并执行以下命令。
cordova插件添加cordova-plugin-whitelist
更多信息请引用此URL
谢谢, 埃兰迪卡。
关于android - Web API 调用在设备模式下失败,但在 Cordova 的模拟器中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31698666/