角美元http.Post then方法不起作用

angular $http.post then method doesn't work

本文关键字:方法 不起作用 then Post 美元 http      更新时间:2023-09-26

我使用这个函数登录,但是我在$http.post方法中遇到了一个问题。我的服务器响应一个400(错误的请求)到不正确的用户名&但是在这个角代码中,loginSuccess函数每次都运行(即使用户名&密码)和其他奇怪的事情是响应是undefined,而我可以在浏览器网络控制台看到响应。

function login (data) {
      var deferred = $q.defer();
      $http.post('api/authentication/login', data)
        .then(loginSuccess, loginFailed);
//        .success(loginSuccess);
      return deferred.promise;
      function loginSuccess (response) {
        console.log(response); // <-- undefinde
        deferred.resolve();
      }
      function loginFailed () {
        deferred.reject();
      }
    }

但是当我使用success方法(已弃用)时,没有问题,当响应错误时登录成功不运行

正如@zeroflagL提到的,问题来自拦截器。我忘了在onResponseError中返回响应。