【问题标题】:web api with AngularJS http get status 0带有 AngularJS http 的 web api 获取状态 0
【发布时间】:2016-10-15 03:31:14
【问题描述】:

当我尝试使用 $http.get() 方法在 AngularJS 中发送请求时出现错误,我得到的答案在此代码中没有数据和状态 0:

客户:

angular.module('myApp', ['MyService']).controller('myCtrl', function ($scope ,HandleService) {

    $scope.AddCar = function () {

        HandleService.GetAll().then(function (result) {
            alert(result.data);
        }, function(err) {
            return alert(err.data);
        });

    }


});



angular.module('MyService', []).factory('HandleService',  ['$http' , function ($http) {

    return {

        GetAll: function(){

            return $http.get('http://localhost:7141/Home/GetAll').then(function (result) {

                return result.data;
            });

        }
    };

}]);

服务器:

[Route("GetAll")]
        [HttpGet]
        public HttpResponseMessage GetAll()
        {
            List<string> names = new List<string>();

            names.Add("1");
            names.Add("2");



            var response = new HttpResponseMessage()
            {
                Content = new StringContent(JArray.FromObject(names).ToString(), Encoding.UTF8, "application/json")
            };

            return response;

        }

当我从 chrome web 调用时,我得到了正确的数据。

【问题讨论】:

  • 控制台说什么

标签: angularjs rest asp.net-web-api


【解决方案1】:

首先在你的控制器中试试这个

    [HttpGet, Route("GetAll")]
    public IHttpActionResult GetAll()
    {
        List<string> names = new List<string>();

        names.Add("1");
        names.Add("2");
        return Ok(names);
    }

假设它是 WebApi2。如果没有

[HttpGet, Route("GetAll")]
public HttpResponseMessage GetAll()
{
    List<string> names = new List<string>();

    names.Add("1");
    names.Add("2");

    return Request.CreateResponse(HttpStatusCode.NoContent);
}

【讨论】:

  • 状态还是返回0
猜你喜欢
  • 2017-05-13
  • 2013-07-29
  • 2012-12-24
  • 2018-07-04
  • 2018-03-25
  • 2015-01-12
  • 1970-01-01
  • 2015-01-24
  • 1970-01-01
相关资源
最近更新 更多