How to store AJAX return to use as a parameter of another function

Asked

Viewed 308 times

1

Main function, which will return the value chave = data.chave:

var chave = '';

$scope.submitForm = function() {
        $http({
          method  : 'POST',
          url     : 'validar.php',
          dataType: 'json',
          data : dados,
          headers : {'Content-Type': 'application/x-www-form-urlencoded'}
         })
         .success(function(data) {
            if (data.errors) {
              $scope.erro = data.errors.errovalidar;
            } else {
              chave = data.chave;
              $('#imprimir-danfe').removeAttr('disabled');
            }
          })
    };

Secondary function, which shall receive as parameter the returned value of the main function (chave):

var imprimir_d = document.getElementById('imprimir');
    imprimir_d.addEventListener('click', function() {
        $http({
          method  : 'POST',
          url     : 'imprimir.php',
          data : chave,
          headers : {'Content-Type': 'application/x-www-form-urlencoded'}
         })
    });

However, the way it is when I do console.debug(chave), within the secondary function and returned "undefined"

  • Your code should work. Because it has a global variable that captures the value. Submit occurs before pressing the button with id "print"?

  • In the first script you did not set the key variable to global. Take the var and leave only: key = ';. Now it will work.

  • Submit occurs before pressing the button with the id "print". @mauhumor

  • The problem still persists. @Williancoqueiro

2 answers

3

You can use the $scope.chave, for the angular is better because it controls the $scope.

and would also create the function of printing in angular calling through the ng-click, and the disable I would use the variable itself of $scope.chave.

make sure your key variable is with data.

<br/>**javascript:**<br/>
$scope.submitForm = function() {<br/>
        $http({<br/>
          method  : 'POST',<br/>
          url     : 'validar.php',<br/>
          dataType: 'json',<br/>
          data : dados,<br/>
          headers : {'Content-Type': 'application/x-www-form-urlencoded'}<br/>
         })<br/>
         .success(function(data) {<br/>
            if (data.errors) {<br/>
              $scope.erro = data.errors.errovalidar;<br/>
            } else {<br/>
              $scope.chave = data.chave;<br/>
              $('#imprimir-danfe').removeAttr('disabled');<br/>
            }<br/>
          })<br/>
    };<br/>
$scope.imprimir = function() {<br/>
        $http({<br/>
          method  : 'POST',<br/>
          url     : 'imprimir.php',<br/>
          data : chave,<br/>
          headers : {'Content-Type': 'application/x-www-form-urlencoded'}<br/>
         })<br/>
    };<br/>
**html** <br/>

input type="button" ng-click="imprimir" ng-disabled="chave==undefined"
  • But the question does not refer to Angular. (put the label after my comment rs).

1


After $('#imprimir-danfe').removeAttr('disabled');.... puts: $('#imprimir-danfe').attr('meu_valor', chave );

And then just take the value of the key:

var imprimir_d = document.getElementById('imprimir');
    imprimir_d.addEventListener('click', function() {
        minha_chave = $('#imprimir-danfe').attr('meu_valor');
        $http({
          method  : 'POST',
          url     : 'imprimir.php',
          data : minha_chave,
          headers : {'Content-Type': 'application/x-www-form-urlencoded'}
         })
    });

Browser other questions tagged

You are not signed in. Login or sign up in order to post.