从Ajax POST请求创建数组

时间:2018-01-07 12:17:37

标签: jquery ajax

我正在使用此代码使用Ajax Post请求创建数组。

$('#accountholderEmail').on('keyup',function() {
        $.ajax({
            type: "POST",
            url: "/section/get_data?getCustomer=1", 
            cache: false, 
            success: function(data){
                var email_array = data;
            }
        });
        console.log(email_array);
});
来自Ajax请求的

data返回一个数组,但它告诉我在console.log(email_array);

中没有定义email_array

如何使用来自ajax请求的返回数据填充此变量

1 个答案:

答案 0 :(得分:1)

你在功能成功时初始化email_array所以想要console.log你必须把它推到功能成功

$('#accountholderEmail').on('keyup',function() {
        $.ajax({
            type: "POST",
            url: "/section/get_data?getCustomer=1", 
            cache: false, 
            success: function(data){
                var email_array = data;
                console.log(email_array);
            }
        });

});