Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Response after POST in AngularJS?

I have java app with API for REST. 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

{
    "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.

And how can I get it and check for next actions.

My 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.

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

</div> 
like image 491
elpofigisto Avatar asked Apr 11 '14 19:04

elpofigisto


People also ask

Which object does the http GET () function return?

response : interceptors get called with http response object. The function is free to modify the response object or create a new one. The function needs to return the response object directly, or as a promise containing the response or a new response object.

How do we pass data and get data using http in angular?

get request Method Syntax: $http. get(url, { params: { params1: values1, params2:values2, params3:values3...... } });

What is $q in AngularJS?

$q is integrated with the $rootScope. Scope Scope model observation mechanism in AngularJS, which means faster propagation of resolution or rejection into your models and avoiding unnecessary browser repaints, which would result in flickering UI. Q has many more features than $q, but that comes at a cost of bytes.

What is $location in AngularJS?

Overview. The $location service parses the URL in the browser address bar (based on the window. location) and makes the URL available to your application. Changes to the URL in the address bar are reflected into $location service and changes to $location are reflected into the browser address bar.


2 Answers

$http (documentation) returns a promise with two additional methods as well. success which you are handling correctly and error which you do not have defined but you can easily chain after your success function.

$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:

$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.

like image 127
Brocco Avatar answered Oct 12 '22 05:10

Brocco


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));
    });`
like image 32
Siva Avatar answered Oct 12 '22 04:10

Siva