DownDown
DownDown

Reputation: 374

Use variable outside the success function from an ajax/jquery call

I have the following code

var test;

 $.ajax({
    type: "GET",
    url: "../views/person/controller.php?actor=person&action=checkAge",
    data: "age=" + value,
    success: function(msg){
        console.log(msg);
        test = msg; 
    },
});

Validate.fail(test);

Now the test var should give true of false like the console does say. But test var gives me undefined why?

Upvotes: 20

Views: 42125

Answers (5)

var test;
 $.ajax({
    type: "GET",
    async: false,
    url: "../views/person/controller.php?actor=person&action=checkAge",
    data: "age=" + value,
    success: function(msg){
        console.log(msg);
        test = msg; 
    },
});
Validate.fail(test);

//Make your ajax function synchronous, set the json parameter "async: false", so javascript has to wait until test is assigned a value.

Upvotes: 11

JohnP
JohnP

Reputation: 50009

 var test; // <-- (1) This code runs first  
 $.ajax({  // <-- (2) Then this runs  
    type: "GET",
    url: "../views/person/controller.php?actor=person&action=checkAge",
    data: "age=" + value,
    success: function(msg){
        console.log(msg); //<-- (4) Finally this is run. IF your request is a success 
        test = msg; 
    },
 });
 Validate.fail(test); // <-- (3) This runs third  

Look at the order in which the code runs. Your variable is simply not available at that point because it's running when the code is triggered via the callback

Upvotes: 29

Aylian Craspa
Aylian Craspa

Reputation: 466

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var xm;

  $("#txt").ajaxComplete(function(){
    $('#txt').html(xm);
  });

  $("button").click(function(){

    $.ajax({
      url: 'DBresult_load.php',
      dataType: 'html',
      data: { },                 //you can pass values here
      success: function(result) {xm =result;}
    });
  });


});
</script>
</head>
<body>

<div id="txt"><h2>Let AJAX change this text</h2></div>
<button>Change Content</button>
</body>
</html>

Here is the solution for passing values to variable from Ajax request. Hope this helps.

Upvotes: 3

Atticus
Atticus

Reputation: 6720

What happens when you remove "var" before the term "test" when you declare it?

I'm not sure how the call back function is treated with jQuery , as it is wrapped within a few other extended methods.. But the reason i say leave var out in the declaration of the test variable is that var assigns test to be relative to the scope. If your callback is being treated in a certain way, you may lose the scope where test is defined. You may want to drop the var assignment and leave it as a global variable. Perhaps this will make it visible?

EDIT:: Didn't realize you were referencing the term within a function call after the async request -- i would suggest including the last statement within your callback.

:)

Upvotes: -1

maple_shaft
maple_shaft

Reputation: 10463

Probably because Validate.fail(test) occurs immediately after the asynchronous call. Remember it is ASYNCHRONOUS, meaning it executes parallel to javascript running on your page.

Upvotes: 23

Related Questions