douyudouchao6779 2015-03-14 15:27
浏览 70
已采纳

如何在下一个ajax调用中从一个ajax调用中访问返回的变量

I want to access returned variable from one ajax call in another ajax call and want to repeat the second ajax call continuously. my code is below, and the required variables are mentioned in code comments.

<script type="text/javascript" language="javascript">
    $( document ).ready(function() {

        $.ajax({
        url  : "functions.php?id=enter_game&name=<?php echo $name; ?>",
        type : "GET",
        //dataType : 'json',
        success: function(result){
            if(result){
                $("#game_status").html(result["game"]);
                var limit = Object.keys(result).length - 4;
                for(var x = 1; x <= limit ; x++)
                {
                    $("#users").append('<li>'+result[x]["name"]+'</li>');    
                    //$("#users").append('<li>abd</li>');
                }

                // I want to access these variables in next ajax call
                var user_id = result["current_user_id"];
                var word_id = result["word_id"];

                }
                }
                });

                // start updating continuously
                var timer, delay = 1000; // time in milli seconds
                    timer = setInterval(function(){

                        $.ajax({
                          type    : 'GET',

                          // the variables from previous ajax call result should be avaialable here
                          // and only this ajax call should be made upon delay time.
                          // previous ajax call should not be made more than once.
                          url     : 'functions.php?user_id='+user_id+'&word_id='+word_id,
                          //dataType: 'json',
                          success : function(data){
                                      if(data){
                                        $("#game_status").html(Math.floor((Math.random() * 10) + 1));
                                      }
                                    },
                            error: function( xhr, status, errorThrown ) {
                            alert( "Sorry, there was a problem! Error: " + errorThrown + ", Status: " + status + ", xhr: ," + xhr);
                            console.log( "Error: " + errorThrown );
                            console.log( "Status: " + status );
                            console.dir( xhr );
                            }
                        });
                    }, delay);
                });
</script>

Any help would be much appreciated.

  • 写回答

4条回答 默认 最新

  • duanquan1207 2015-03-14 15:33
    关注

    try below code . declare variables out side function. read more about variable scope in java script here

     <script type="text/javascript" language="javascript">
    
     var user_id;
     var word_id;
    
    $( document ).ready(function() {
    
        $.ajax({
        url  : "functions.php?id=enter_game&name=<?php echo $name; ?>",
        type : "GET",
        //dataType : 'json',
        success: function(result){
            if(result){
                $("#game_status").html(result["game"]);
                var limit = Object.keys(result).length - 4;
                for(var x = 1; x <= limit ; x++)
                {
                    $("#users").append('<li>'+result[x]["name"]+'</li>');    
                    //$("#users").append('<li>abd</li>');
                }
    
                // I want to access these variables in next ajax call
                user_id = result["current_user_id"];
                word_id = result["word_id"];
    
                }
                }
                });
    
                // start updating continuously
                var timer, delay = 1000; // time in milli seconds
                    timer = setInterval(function(){
    
                        $.ajax({
                          type    : 'GET',
    
                          // the variables from previous ajax call result should be avaialable here
                          // and only this ajax call should be made upon delay time.
                          // previous ajax call should not be made more than once.
                          url     : 'functions.php?user_id='+user_id+'&word_id='+word_id,
                          //dataType: 'json',
                          success : function(data){
                                      if(data){
                                        $("#game_status").html(Math.floor((Math.random() * 10) + 1));
                                      }
                                    },
                            error: function( xhr, status, errorThrown ) {
                            alert( "Sorry, there was a problem! Error: " + errorThrown + ", Status: " + status + ", xhr: ," + xhr);
                            console.log( "Error: " + errorThrown );
                            console.log( "Status: " + status );
                            console.dir( xhr );
                            }
                        });
                    }, delay);
                });
    

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?