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

后端 未结 5 788
北海茫月
北海茫月 2020-11-27 22:04

I have the following code

var test;
     $.ajax({
        type: \"GET\",
        url: \"../views/person/controller.php?actor=person&action=checkAge\",
           


        
5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-27 23:02

     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

提交回复
热议问题