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条)

报告相同问题?

悬赏问题

  • ¥50 远程桌面打开Mastercam、没有许可证、物理机打开正常
  • ¥15 ubuntu安装gdal后java读取tif文件报错
  • ¥15 请问lammps怎么做两种金属连接的原子浓度分布图
  • ¥15 求jacquard数据集
  • ¥15 w10部分软件不能联网
  • ¥15 关于安装hbase的问题(操作系统-windows)
  • ¥15 novnc连接pve虚拟机报错安全协议不支持262
  • ¥15 设备精度0.03给多少公差能达到CPK1.33
  • ¥15 qt+ffmpeg报错non-existing PPS 0 referenced
  • ¥50 MacOS 使用虚拟机安装k8s