开发者

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&开发者_JAVA百科quot;,
    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?


 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


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.


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.


<!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.


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.

:)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜