首页 文章

使用angular $ http得到asp net web api

提问于
浏览
1

我试图使用asp web api使用angular填充html表 . 如果我在firefox中调试一切都很好(我假设因为我的web服务是在json中返回的)但是在ie和chrome中它没有加载(web服务在这些浏览器中返回xml) . 在webapiconfig中,我试图通过添加来使服务返回json .

Dim appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(Function(t) t.MediaType = "application/xml")
    config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType)

这似乎工作时,我导航到所有浏览器中的api它返回json但是$ http get现在仍在使用chrome和ie .

即我得到以下错误

Unhandled exception at line 21, column 406 in http://localhost:53175/Scripts/angular.min.js

    0x800a139e - JavaScript runtime error: [$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(function (data) {
        $scope.data.products = 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>

这里是正在返回的j_son(如果它看起来很熟悉,我正在通过专业的Angular书)

2 回答

  • 1

    带有1个参数的angular.module返回带有该名称的模块 - 您需要使用依赖模块列表定义模块(如果没有,则需要为空数组),如下所示:

    angular.module("api/products", [])...
    

    引用的错误给出了一个错误,其中包含有关问题的详细信息(angular的错误信息非常好):https://docs.angularjs.org/error/$injector/nomod?p0=api%2Fproducts

  • 0

    对不起,这是c#代码,但它应该说明仅从web api返回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;
            }
        }
    

    所以你要用Json内容返回HttpResponseMessage .

    我也在类似的情况下,我只需要从一个MVC控制器返回数据,这更容易:

    public ActionResult Get(string guid)
            {
                var profileVm = _profileService.Get(guid);
                if (profileVm != null)
                {
                    return Json(profileVm, JsonRequestBehavior.AllowGet);
                }
    
                return new HttpNotFoundResult();
            }
    

相关问题