Angular - http post-error转到success函数

时间:2016-09-17 17:25:32

标签: angularjs

我将帖子查询发送到服务器(Angular v1.5.5),服务器返回错误500.但是为什么该错误会转到success回调而不是error回调函数? Firebug向我显示success消息,而没有error消息。

$http.post('someURL', someData)
     .success(function (data1) {
          console.log('success');

          return data1;
      })
      .error(function (data2) {
          console.log('error');

          return data2;
      });

1 个答案:

答案 0 :(得分:1)

这是angular 1.5.5的正确语法:

// Simple GET request example:
$http({
  method: 'GET',
  url: '/someUrl'
}).then(function successCallback(response) {
    // this callback will be called asynchronously
    // when the response is available
  }, function errorCallback(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });

参考:https://code.angularjs.org/1.5.5/docs/api/ng/service/ $ http