gpt4 book ai didi

android - Web API 调用在设备模式下失败但在 Cordova 的模拟器中工作

转载 作者:搜寻专家 更新时间:2023-11-01 08:42:02 25 4
gpt4 key购买 nike

在我的项目中,我在云服务器中部署了我的 web api 并从该服务获取数据。在 ripple 模拟器中,它运行完美。但是当我在设备模式(Android 选项卡)中调试时,它可以调用该服务。它也没有显示任何特定的错误消息。

在我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com