1

Below is my angular controller but in it, my promise doesn't seem to work. console.log isn't showing response nor is alert being shown over here.

myapp.controller('contrl6', ['$scope','$q','$localStorage','$http',
  function ($scope,$q,$localStorage,$http) {

    $scope.myXhr = function(){

    var q = $q.defer();

     $http({
        method: 'GET',
         url: '/api/session_data'
        })
        .success(function(response){
            console.log(response);
            q.resolve('request successful');
        })
        .error(function(response){
          console.log("error");
            q.reject('ERROR');
        });


    return q.promise;
}
console.log("hi");

var myPromise = $scope.myXhr();
  myPromise.then(function(resolve){
        alert(resolve);
        console.log(resolve);
        }, function(reject){
        alert(reject);
        console.log(reject);      
    });


  }]);
4
  • Are any of your console.log() calls showing output (even the 'hi')? If not then perhaps your code isn't wired up correctly and is never being called. Otherwise it looks as though the code should work (though it breaks a lot of the Angular conventions on how to do things). Commented Mar 1, 2016 at 9:38
  • 1
    Also, check the console network log to see whether the request is actually being sent and if so whether it succeeds or fails. Commented Mar 1, 2016 at 9:41
  • hi is showing up on console log,and how is it not according to angular conventions? Commented Mar 1, 2016 at 9:41
  • Apart from success and error being deprecated, you don't need $q here as you can just use the promise from $http. The other conventions to follow would be giving your controller a name that ends with Controller and using $log instead of console.log(). None of which should affect whether the code works. Commented Mar 1, 2016 at 10:47

3 Answers 3

2

Deprecation Notice

The $http legacy promise methods .success and .error have been deprecated. Use the standard .then method instead.1

$scope.myXhrPromise = function(){
    return (
        $http({
            method: 'GET',
            url: '/api/session_data'
        }).then (function onFulfilled (response) {
            console.log(response.data);
            //return data for chaining
            return response.data;
        }).catch (function onRejected (response) {
            console.log(response.status);
            //throw to chain rejection
            throw response;
        })
    );
};
Sign up to request clarification or add additional context in comments.

2 Comments

Deprecated in 1.4.x and higher.
On older versions of AngularJS, the $http service returns promises with both the standard .then and .catch methods as well as the now deprecated .success and .error methods. There is no need to use the deprecated legacy methods when writing code for older versions of AngularJS. Use the standard .then method instead.
2

Inside myxhr nothing is wired together with the result of $http. Don't worry about creating a new promise, because $http already returns one:

$scope.myXhr = function(){

 return $http({
    method: 'GET',
     url: '/api/session_data'
    })
    .then(function(response){
        return response;
    })
    .catch(function(response){
        throw new Error('ERROR');
    });
}

4 Comments

Getting error Cannot read property 'then' of undefined at myPromise.then
As long as you are injecting $http correctly and using return before calling $http, then myPromise.then should always be set to something.
God knows what the error I guess ill just wrote rest of the controller in .success function
@DavinTryon, to return the promise correctly you should change the success and error callbacks to use then and optionally catch.
0

I copy/pasted your code into a Plunkr and it works just fine once I wire it up correctly.

Plunkr example here

As Duncan says, make sure you wire it up correctly, using ng-app and ng-controller. If you can update your question with the HTML markup you're using perhaps we can help further.

1 Comment

God knows what the error I guess ill just wrote rest of the controller in .success function

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.