小编elp*_*sto的帖子

如何在AngularJS中获得POST后的响应?

我有用于REST的API的Java应用程序.我为它做了前端.现在我正在使用授权.

当我发出POST请求应用程序返回我这样的JSON消息:如果登录和传递是正确的

{
    "result": {
        "token": "shgvojhhsifav37o5a3sebc3if"
    }
} 
Run Code Online (Sandbox Code Playgroud)

如果他们不对:

{
    "error": {
        "code": 10,
        "message": "Incorrect login or password"
    }
}
Run Code Online (Sandbox Code Playgroud)

我可以在浏览器中看到响应,但不能在JavaScript代码中使用它.

我怎样才能得到它并检查下一步行动.

我的JavaScript:

controllers.controller('userAuthCtrl', ['$scope','$http',
  function($scope, $http){
        $http({
            method: 'POST',
            url: '/rest/api/auth/login',
            data: '?login=test&password=passwo3rd',
            headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
        }).
            success(function(data, status, header, config) {
                console.log(header());
                console.log(config);

                $scope.dataU = data;
                $scope.statusU = status;
            }).error(function(data, status, header, config){
                console.log(header());
                console.log(config);
            })

  }
]);
Run Code Online (Sandbox Code Playgroud)

我的测试HTML现在很简单.

<div class="testCont" ng-controller="userAuthCtrl">

</div> 
Run Code Online (Sandbox Code Playgroud)

javascript angularjs

8
推荐指数
1
解决办法
4万
查看次数

标签 统计

angularjs ×1

javascript ×1