3

I'm using this and this reference, to return data from an http-request inside a function:

function getdetails(id) {

            var datafordetails = {
                data1: {
                    item1: "",
                    item2: ""
                },
                data2: {
                    item3: "",
                    ID: id
                }
            };

            var jsonDataDetails = JSON.stringify(datafordetails);
            return $http({
                url: "http://...",
                method: "POST",
                data: jsonDataDetails,
                dataType: "json",
                timeout: 5000,
                contentType: 'application/json; charset=utf-8'
            })
                .then(function (res) {
                    var data = res.data;
                    responsedata=data.d;
                    return responsedata;

                },function (error) {
                    $scope.status = status;
                    console.log("Unable to update. No internet?");
                })
        }

My goal is that var testing = getdetails('12345'); gives me the responsedata for that ID, but I'm getting back Promise {$$state: Object...

What am I doing wrong? Thanks a lot!

1 Answer 1

3

The output you are getting is nothing but a promise object which has been returned by the $http.get method. Basically you need to put .then function over getdetails2 function for getting data from the promise object when it resolve/reject.

Code

getdetails2('12345').then(function(data){ //success callback
   //you will get data here in data parameter of function,
   var testing = data;
}, function(error){ //error callback
   console.log(error)
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.