简体   繁体   English

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

[英]How to get Response after POST in AngularJS?

I have java app with API for REST. 我有用于REST的API的Java应用程序。 And I make front-end for it. 我为它做了前端。 Now I'm working with authorization. 现在我正在使用授权。

When I make POST request app returns me JSON message like this: If login and pass is right 当我发出POST请求应用程序返回我这样的JSON消息:如果登录和传递是正确的

{
    "result": {
        "token": "shgvojhhsifav37o5a3sebc3if"
    }
} 

And if they are not right: 如果他们不对:

{
    "error": {
        "code": 10,
        "message": "Incorrect login or password"
    }
}

I can see the response in browser, but can't take it to use in JavaScript code. 我可以在浏览器中看到响应,但不能在JavaScript代码中使用它。

And how can I get it and check for next actions. 我怎样才能得到它并检查下一步行动。

My 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);
            })

  }
]);

My test HTML is easy now. 我的测试HTML现在很简单。

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

</div> 

$http ( documentation ) returns a promise with two additional methods as well. $ http( 文档 )也返回一个带有两个附加方法的promise。 success which you are handling correctly and error which you do not have defined but you can easily chain after your success function. 您正确处理的success和您没有定义的error ,但您可以轻松地在success功能之后进行链接。

$http({[options]})
    .success(function(data, status, headers, config){})
    .error(function(data, status, headers, config){});

If your web response is still returning a status of 200 for success or failure then you can handle it thusly in your success handler: 如果您的Web响应仍然返回成功或失败状态200,那么您可以在成功处理程序中处理它:

$http({})
    .success(function(data, status, headers, config){
        /*called for result & error because 200 status*/
        if (data.result){
            //handle success here
        } else if (data.error {
            //handle error here
    })
    .error(function(data, status, headers, config){
        /*handle non 200 statuses*/
    });

Based on comments, you will also need to change this: 根据评论,您还需要更改此内容:

data: '?login=test&password=passwo3rd

to: 至:

data: {login: 'test', password: 'passwo3rd'}

This is because it is not a query string but instead part of the request body in JSON format. 这是因为它不是查询字符串,而是JSON格式的请求体的一部分。

It might be helpful for you. 它可能对你有所帮助。

 $http.post("Your URL").success(function(data) {
        // success
        here 


    }).error(function(error) {
        // error
        console.log(JSON.stringify("Failed to get t&c: " + error));
    });`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM