使用 angular $http 获取 asp 网络 api
using angular $http get with asp net web api
我正在尝试使用 asp 网络 api 通过 angular 填充 html table。如果我在 firefox 中调试,一切都很好(我假设是因为我的网络服务在 json 中被 return 编辑)但是在 ie 和 chrome 中它不会加载(网络服务 returns xml 在这些浏览器中)。在 webapiconfig 中,我试图通过添加总是使服务 return json。
Dim appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(函数(t) t.MediaType = "application/xml")
config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType)
当我在所有浏览器中导航到 api 时,它似乎工作 returning json 但是 $http get 现在仍在 chrome 中工作即
在 ie 中我得到以下错误
http://localhost:53175/Scripts/angular.min.js 中第 21 行第 406 列未处理的异常
0x800a139e - JavaScript 运行时错误:[$injector:nomod] http://errors.angularjs.org/1.3.13/$injector/nomod?p0=api%2Fproducts
这是我的
angular.module("api/products").constant("dataUrl", "sportstore.json").controller("sportsStoreCtrl", function ($scope, $resource, dataUrl) {
$scope.data = {};
var resultPromise = $resource(dataUrl);
resultPromise.success(函数(数据){
$scope.data.产品=数据;
})
});
有什么想法吗?
附加信息
这是我的 api 控制器
<pre>
Imports System.Net
Imports System.Web.Http
Imports apitoform.productRepository
Namespace Controllers
Public Class productController
Inherits ApiController
Private repo As productRepository = productRepository.Current
Public Function GetAllProducts() As IEnumerable(Of product)
Return repo.GetAll()
End Function
End Class
End Namespace
</pre>
这是正在 return 编辑的 j_son(如果看起来很熟悉,我正在阅读专业 Angular 的书)
angular.module 带有 1 个参数 returns 具有该名称的模块 - 您需要使用依赖模块列表(如果 none 则为空数组)来定义模块,如下所示:
angular.module("api/products", [])...
引用的错误给出了错误的详细信息(angular对他们的错误消息非常了解):https://docs.angularjs.org/error/$injector/nomod?p0=api%2Fproducts
抱歉,这是 C# 代码,但它应该仅从 Web api 说明 returning Json 的基本思想。这是我的一个项目的实际代码。
[Route("api/users/getbyemail/")]
public HttpResponseMessage GetByEmail(string email)
{
try
{
var result = _userService.GetByEmail(email);
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, "value");
response.Content = new ObjectContent(typeof(IEnumerable<UserViewModel>), result ?? new List<UserViewModel>(), new JsonMediaTypeFormatter());
response.Headers.Add("Access-Control-Allow-Origin", "*");
return response;
}
catch (Exception ex)
{
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
response.Headers.Add("Access-Control-Allow-Origin", "*");
return response;
}
}
所以您正在 returning HttpResponseMessage 与 Json 内容。
在我只需要 return 来自一个 MVC 控制器的数据的情况下,我也在做类似的事情,这更容易:
public ActionResult Get(string guid)
{
var profileVm = _profileService.Get(guid);
if (profileVm != null)
{
return Json(profileVm, JsonRequestBehavior.AllowGet);
}
return new HttpNotFoundResult();
}
我正在尝试使用 asp 网络 api 通过 angular 填充 html table。如果我在 firefox 中调试,一切都很好(我假设是因为我的网络服务在 json 中被 return 编辑)但是在 ie 和 chrome 中它不会加载(网络服务 returns xml 在这些浏览器中)。在 webapiconfig 中,我试图通过添加总是使服务 return json。
Dim appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(函数(t) t.MediaType = "application/xml") config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType)
当我在所有浏览器中导航到 api 时,它似乎工作 returning json 但是 $http get 现在仍在 chrome 中工作即
在 ie 中我得到以下错误
http://localhost:53175/Scripts/angular.min.js 中第 21 行第 406 列未处理的异常 0x800a139e - JavaScript 运行时错误:[$injector:nomod] http://errors.angularjs.org/1.3.13/$injector/nomod?p0=api%2Fproducts
这是我的
angular.module("api/products").constant("dataUrl", "sportstore.json").controller("sportsStoreCtrl", function ($scope, $resource, dataUrl) { $scope.data = {}; var resultPromise = $resource(dataUrl); resultPromise.success(函数(数据){ $scope.data.产品=数据; }) });
有什么想法吗?
附加信息
这是我的 api 控制器
<pre>
Imports System.Net
Imports System.Web.Http
Imports apitoform.productRepository
Namespace Controllers
Public Class productController
Inherits ApiController
Private repo As productRepository = productRepository.Current
Public Function GetAllProducts() As IEnumerable(Of product)
Return repo.GetAll()
End Function
End Class
End Namespace
</pre>
这是正在 return 编辑的 j_son(如果看起来很熟悉,我正在阅读专业 Angular 的书)
angular.module 带有 1 个参数 returns 具有该名称的模块 - 您需要使用依赖模块列表(如果 none 则为空数组)来定义模块,如下所示:
angular.module("api/products", [])...
引用的错误给出了错误的详细信息(angular对他们的错误消息非常了解):https://docs.angularjs.org/error/$injector/nomod?p0=api%2Fproducts
抱歉,这是 C# 代码,但它应该仅从 Web api 说明 returning Json 的基本思想。这是我的一个项目的实际代码。
[Route("api/users/getbyemail/")]
public HttpResponseMessage GetByEmail(string email)
{
try
{
var result = _userService.GetByEmail(email);
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, "value");
response.Content = new ObjectContent(typeof(IEnumerable<UserViewModel>), result ?? new List<UserViewModel>(), new JsonMediaTypeFormatter());
response.Headers.Add("Access-Control-Allow-Origin", "*");
return response;
}
catch (Exception ex)
{
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
response.Headers.Add("Access-Control-Allow-Origin", "*");
return response;
}
}
所以您正在 returning HttpResponseMessage 与 Json 内容。
在我只需要 return 来自一个 MVC 控制器的数据的情况下,我也在做类似的事情,这更容易:
public ActionResult Get(string guid)
{
var profileVm = _profileService.Get(guid);
if (profileVm != null)
{
return Json(profileVm, JsonRequestBehavior.AllowGet);
}
return new HttpNotFoundResult();
}