1

Here is my code of angularjs file

$scope.httpPost = function (url, json_form_data) {

    $http.post(url, json_form_data)
            .then(function (response) {

                $scope.responseData = response.data;
                return $scope.responseData;
            }, function (response) {
                $scope.content = "Something went wrong";
            });
};

Here is the function in which i am calling the above function

$scope.getAuthToken = function (account_type, auth_type, email, type_of_request) {  // type of request means login or signUp


    var account_type = account_type;

    var form_data = {
        'email': email,
        "auth_type": auth_type,
        "account_type": account_type
    };

    $scope.responseData = $scope.httpPost($scope.authTokenUrl, form_data);
    console.log("value of res");
    console.log($scope.responseData);
};

The output of the above code is

value of res
loginAndSignup.js:138 undefined

My question is that How can i access that value which function returning because i needed that value.

I tried the following solution

$scope.httpPost = function (url, json_form_data) {

return $http.post(url, json_form_data)
        .then(function (response) {

            return  response;

        }, function (response) {
            $scope.content = "Something went wrong";
        });
};



$scope.login = function (email, auth_token, auth_type, account_type) {

var password = $scope.password;

var form_data = {
    //form data
};

var url = $scope.loginUrl;
$scope.httpPost(url, form_data)
        .then(function (response) {

            return  response;

        }, function (response) {
            $scope.content = "Something went wrong";
        });
user979879
  • 93
  • 1
  • 10
  • you need to have `return $http.post(...)` in order to return a Promise with your data. Then you need to resolve it with: `$scope.httpPost(...).then(function(res){$scope.responseData = res.data; console.log($scope.responseData)})` – Aleksey Solovey Sep 24 '18 at 08:04
  • Try using factory. https://docs.angularjs.org/guide/providers – Onur Önder Sep 24 '18 at 08:13
  • @AlekseySolovey i can't understand what you want to say please say clearly thanks – user979879 Sep 24 '18 at 08:38
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Aleksey Solovey Sep 24 '18 at 08:41
  • @AlekseySolovey No, i would not want to use javascript function and Ajax at all , i am only want to use angular js – user979879 Sep 24 '18 at 08:45
  • @AlekseySolovey https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call this question does not help me at all because it is totally different what i want – user979879 Sep 24 '18 at 08:46
  • @user979879 the answer there explains the general idea behind asynchronous callbacks. You need to return the value (`return $scope.responseData;`) AND the promise itself (`return $http.post(...)`). Then, to get any info from that Promise, you need to **resolve** it with `.then()`, for example: `scope.httpPost(...).then((res) =>{$scope.responseData = res})` – Aleksey Solovey Sep 24 '18 at 08:52
  • @AlekseySolovey but i want that the function httpPost return me the final result that is possible? – user979879 Sep 24 '18 at 08:58
  • @user979879 no, you have to resolve it every time you want to use any values from the promise – Aleksey Solovey Sep 24 '18 at 09:00
  • @AlekseySolovey when i tried your code it generate an error that:-> then is undefined . – user979879 Sep 24 '18 at 09:09
  • @user979879 maybe `Cannot read property 'then' of undefined`? Are you returning the promise like I said? Is the function `$scope.httpPost` used correctly? – Aleksey Solovey Sep 24 '18 at 09:15
  • @AlekseySolovey can i provide my code which you tell me to do – user979879 Sep 24 '18 at 10:35
  • i recently update my code with which you tell me to apply please see the code and tell me i am correctly doing that or not and correct me if not – user979879 Sep 24 '18 at 10:44

1 Answers1

0

using $q (A service that helps you run functions asynchronously, and use their return values (or exceptions) when they are done processing.)

click here for more detail

used this function

$scope.httpPost = function (url, json_form_data) {
    var d = $q.defer();
    $http.post(url, json_form_data)
            .then(function (response) {
                   d.resolve(response);
             }, function (response) {
               d.reject(response);
            });

   return d.promise;
};
Jignesh Joisar
  • 13,720
  • 5
  • 57
  • 57