【问题标题】:AngularJS $http success function not workingAngularJS $http成功功能不起作用
【发布时间】:2017-07-13 03:01:28
【问题描述】:

我是 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: {}

我发现了一些类似的主题,但我无法解决我的问题。

【问题讨论】:

    标签: angularjs angularjs-http


    【解决方案1】:

    successerror 在 1.6 中被删除。有一些原因:Why are angular $http success/error methods deprecated? Removed from v1.6?

    使用 thencatchfinally,它们是 Promise 的标准。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-23
      • 2014-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-17
      相关资源
      最近更新 更多