AngularJS $ http成功功能不起作用
问题描述:
我在AngularJS上非常新,我正在按照教程向服务器发送REST请求。这是我的代码
angular.module('myApp')
.controller('MainCtrl', ['$scope','$window','$http', function($scope,$window,$http)
{
$scope.submitForm = function() {
var url = 'http://localhost:8080/Server/config/start';
var request = $http({
method: 'POST',
url: url,
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
transformRequest: function(obj) {
var str = [];
for(var p in obj)
str.push(encodeURIComponent(p) + '=' + encodeURIComponent(obj[p]));
return str.join('&');
},
data: {gsvalue: $scope.name}}).success(function() {});
}
}]);
我在服务器端正确接收请求,但我无法获得响应,成功函数不会被调用。我也得到了控制台
Error: $http(...).success is not a function
和
Error: JSON.parse: unexpected character at line 1 column 1 of the JSON data
.......
Possibly unhandled rejection: {}
两个警告我发现了一些类似的主题,但我没能解决我的问题与他们。
答
success
和error
在1.6中被删除。有一些原因:Why are angular $http success/error methods deprecated? Removed from v1.6?。
使用then
,catch
和finally
,这是Promises的标准。