在AJAX函数中声明变量并稍后调用它(jQuery)

时间:2013-09-12 18:36:36

标签: javascript jquery ajax

我有一个正常工作的AJAX函数,但我似乎无法访问我在页面后面的函数中声明的变量。如果你查看下面的代码,你会看到我在函数中警告变量number_rows,但当我尝试将它记录在函数之外时,它会以'undefined'的形式返回。

var newData = "user_id=" + id;

    $.ajax({
        type: 'POST', // HTTP method POST or GET
        url: 'ajax.php', //Where to make Ajax calls
        dataType:'text', // Data type, HTML, json etc.
        data:newData, //post variables
        success:function(response){

            var number_rows = response;
           alert(number_rows);

        },
        error:function (xhr, ajaxOptions, thrownError){
            alert(xhr + " " + ajaxOptions + " " + thrownError); //throw any errors
        }
    });

console.log(number_rows);

我知道我的范围可能不合适。我试图在成功函数中移动我的所有代码,但这导致了许多其他问题,所以最简单的方法是将响应变量作为一个全局变量,我可以在页面的其余部分代码中使用它。我也试过这样的事情:

success:function(response){

        $('body').append('<span class="ajax_response" id="' + response + '"></span>');

    }

var number_rows = $('.ajax_response').attr('id');
console.log(number_rows);

但由于某种原因,它无法立即获取ID值。我可以确认跨度确实使用正确的ID值,但由于某种原因它无法正确处理它。对此的任何帮助将不胜感激。

谢谢!

3 个答案:

答案 0 :(得分:-2)

更改了变量的范围 更改 async:false 或等到服务器收到响应,然后使用该值。

var newData = "user_id=" + id;
var number_rows = "";  //Declare Here

    $.ajax({
        type: 'POST', // HTTP method POST or GET
        url: 'ajax.php', //Where to make Ajax calls
        dataType:'text', // Data type, HTML, json etc.
        data:newData, //post variables
        async: false,
        success:function(response){

            number_rows = response;  //assign here
           alert(number_rows);

        },
        error:function (xhr, ajaxOptions, thrownError){
            alert(xhr + " " + ajaxOptions + " " + thrownError); //throw any errors
        }
    });

console.log(number_rows);  //Use here

答案 1 :(得分:-2)

是的,您的范围已关闭。如果您在函数内部定义变量,则无法在外部访问它。你的ajax调用也是async和你的任何代码。

将你的console.log放在success函数中,否则即使你在函数之外全局声明变量,它也可能会记录undefined。

var newData = "user_id=" + id;
var number_rows;
$.ajax({
    type: 'POST', // HTTP method POST or GET
    url: 'ajax.php', //Where to make Ajax calls
    dataType:'text', // Data type, HTML, json etc.
    data:newData, //post variables
    success:function(response){
        number_rows = response;
        alert(number_rows);
        console.log(number_rows); // here, so we are sure it's set. 
        },
        error:function (xhr, ajaxOptions, thrownError){
            alert(xhr + " " + ajaxOptions + " " + thrownError); //throw any errors
        }
    });

答案 2 :(得分:-2)

只需添加到您的配置中:

async:false

您的代码可能如下所示:

var newData = "user_id=" + id;
var number_rows = ""; //This is a global variable

    $.ajax({
        type: 'POST', 
        url: 'ajax.php',
        dataType:'text', 
        data:newData,
        async:false, //<------This is the option you must add
        success:function(response){

            number_rows = response;  //<----- receive data from the server
           alert(number_rows);

        },
        error:function (/*stuff here*/){
            //stuff here
        }
    });

  //Check data
   console.log(number_rows); 
祝你好运^^!

相关问题