Javascript 在AngularJS+;WebAPI
我试图在WebAPI上使用AngularJS打印类别列表。我有下面的页面,当我导航到它时,我会收到包含“-1”的警告消息Javascript 在AngularJS+;WebAPI,javascript,angularjs,asp.net-web-api,Javascript,Angularjs,Asp.net Web Api,我试图在WebAPI上使用AngularJS打印类别列表。我有下面的页面,当我导航到它时,我会收到包含“-1”的警告消息 var myApp=angular.module('myApp',[]); myApp.service('categoriesService',函数($http){ 删除$http.defaults.headers.common['X-Requested-With']; this.getData=函数(){ //$http()返回一个$promise,我们可以使用它添加处理
var myApp=angular.module('myApp',[]);
myApp.service('categoriesService',函数($http){
删除$http.defaults.headers.common['X-Requested-With'];
this.getData=函数(){
//$http()返回一个$promise,我们可以使用它添加处理程序
.然后()
返回$http({
方法:“GET”,
网址:'https://www.example.com/api/categories'
});
}
});
myApp.controller('CategoriesCtrl',函数($scope,categoriesService){
$scope.data=null;
categoriesService.getData().then(函数(响应){
$scope.data=响应;
},功能(回应){
警报(响应状态);
});
});
-
{{category.Name}
我做错了什么?我试过这里的样品:
这里呢
您没有将
分类服务
注入控制器,而是注入了数据服务
。试试这个
myApp.controller('CategoriesCtrl', function ($scope, categoriesService) {
$scope.data = null;
categoriesService.getData().then(function (response) {
$scope.data = response;
}, function (response) {
alert(response.status);
});
});
您没有将
分类服务
注入控制器,而是注入了数据服务
。试试这个
myApp.controller('CategoriesCtrl', function ($scope, categoriesService) {
$scope.data = null;
categoriesService.getData().then(function (response) {
$scope.data = response;
}, function (response) {
alert(response.status);
});
});
您的服务返回承诺,但当承诺解决时,不会返回任何数据:
this.getData = function () {
return $http.get('https://www.example.com/api/categories').then(
function(response) {
console.log(response);
return response;
},
function(error) {
console.log(error);
return error;
}
});
}
您的服务返回承诺,但当承诺解决时,不会返回任何数据:
this.getData = function () {
return $http.get('https://www.example.com/api/categories').then(
function(response) {
console.log(response);
return response;
},
function(error) {
console.log(error);
return error;
}
});
}
非常感谢你的提醒!但是结果仍然是一样的,用“-1”信息提醒非常感谢提醒!但结果仍然是一样的,用“-1”消息提醒谢谢!CORS也有问题。在遵循了本主题中的建议之后,我也配置了它,并且一切正常。谢谢!CORS也有问题。在遵循了本主题中的建议之后,我也配置了它,并且一切正常。