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

后端 未结 5 755
北海茫月
北海茫月 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 22:40
    <!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.

    0 讨论(0)
  • 2020-11-27 22:43

    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.

    0 讨论(0)
  • 2020-11-27 22:43

    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 讨论(0)
  • 2020-11-27 22:45
    enter code here 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.

    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题