【发布时间】: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