【问题标题】:Post request return wrong status code发布请求返回错误的状态码
【发布时间】:2016-09-14 21:03:48
【问题描述】:

我正在使用 AnugularJS 和 ExpressJS 创建一个登录屏幕。我创建了在 Postman 插件中运行良好的身份验证 API URL。当我在 Angular 上发出 post 请求时,它会返回意外的状态代码。

查看

<input ng-model="login.name" type="text" placeholder="Admin User">
<input ng-model="login.password" type="password" placeholder="Password">
<div ng-click="logIn(login.name, login.password)">Login</div>

管理员控制器

$scope.logIn = function logIn(username, password){
    if(username !== undefined && password !== undefined) {
        userservice.logIn(username, password).success(function(data){
            authenticationservice.isLogged = true;
            $window.sessionStorage.token = data.token;
            $location.path("/dashboard");
        }).error(function(status, data){
            console.log(status);
            console.log(data);
        });
    }
};

用户服务

.factory('userservice',['$http','$location', function ($http,$location) {
  return {
      logIn: function(name, password){
          return $http.post($location.protocol() + '://'+ $location.host() +':'+  $location.port() +'/api/authenticate',{name: name, password: password});
      }
  }
}]);

问题

使用邮递员 POST,

http://localhost:8080/api/authenticate

使用 {name: "abc","password:"123"} 应该返回

{成功:假,状态:401};

但是 userservice.logIn(username, password) 从管理员控制器每次返回状态 200。

【问题讨论】:

    标签: angularjs authentication postman


    【解决方案1】:

    这是所有状态代码的列表:https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

    401代表未授权,这可能意味着您的登录数据不正确或服务器端存在错误。

    【讨论】:

      猜你喜欢
      • 2019-05-18
      • 2019-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多