JavaScript jquery.Ajax在成功后循环没有工作


JavaScript jquery.ajax in success after loop nothing working

我需要将一些值传递给Javascript中的其他函数。我已经从另一个PHP文件的值,但我不能把它传递给任何其他Javascript函数。在FOR循环之后的SUCCESS中没有任何工作。需要帮忙吗?

url1 = 'http://localhost:81/Dashboard/admin/inc/dashboard.php';
$.ajax({
    url: url1,
    type: "POST",
    dataType: "json",
    success: function(data)
    {
        for(var i = 0; i <= 11; i++) 
        {
            Month = data[i].Month;
            Sales = data[i].Sales;
            if (i == 0) global_ij = "[" + Month + "," + Sales + "]";
            else global_ij = global_ij + "," + "[" + Month + "," + Sales + "]";
        }
        alert ("hello"); // this alert is also not working
    }
});

看起来你的PHP返回了8个元素,而在你的成功方法中,你的循环迭代了11个元素,导致了错误。

我分离了成功函数并尝试使用您发布的数据,并将循环中的11替换为data.length。看看下面的代码:

http://codepen.io/anon/pen/OyXzGb?编辑= 011

注意我添加了

var月;

var销售;

将这些临时变量保留在函数的作用域中。

您可能需要检查Data以查看它是否是一个正确的数组,以捕获错误。在这行之前:

for(var I = 0;我& lt; data.length ;我+ +)

最终输出和要尝试的内容:

var global_ij="";
function processData(data) {
    var Month;
    var Sales;
    for(var i = 0; i < data.length; i++) 
    {
        Month = data[i].Month;
        Sales = data[i].Sales;
        if (i == 0) global_ij = "[" + Month + "," + Sales + "]";
        else global_ij = global_ij + "," + "[" + Month + "," + Sales + "]";
        console.log(global_ij);
    }
    return global_ij;
}

在没有ajax的情况下先试试这个函数:

processData([{"Month"1","Year":"2015","Sales":"19746.81";},
{"Month"2","Year":"2015","Sales":"17902.26";},{"Month"3","Year":"2015","Sales":"19223.84";},{"Month"4","Year":"2015","Sales":"18840.88";},{"Month"5","Year":"2015","Sales":"19889.97";},{"Month"6","Year":"2015","Sales":"18509.85";},{"Month"7","Year":"2015","Sales":"1886.81";},{"Month"8","Year":"2015","Sales":"1740.34"}]);

你可能想使用.done()

这是因为您正在执行异步 AJAX操作。换句话说,您对变量Month, Sale, global_ij的赋值只能在特定success函数的作用域中可用,而不能在之外

一个解决方法是async: false添加到AJAX调用,这将强制它退出异步行为,因此允许您将分配给这些变量的值提供给所有剩余的代码块:

$.ajax({
url: url1,
async: false,
type: "POST",
dataType: "json",
success: function(data)
{
    for(var i = 0; i <= 11; i++) 
    {
        Month = data[i].Month;
        Sales = data[i].Sales;
        if (i == 0) global_ij = "[" + Month + "," + Sales + "]";
        else global_ij = global_ij + "," + "[" + Month + "," + Sales + "]";
    }
}
});

jQuery的AJAX调用返回promise对象,强制.done(), .fail()等方法

另一方面,你也可以得到一个承诺从AJAX调用(你可以在Javascript代码的任何地方传递),当承诺得到解析调用它的.done()处理程序。
var promise = $.ajax({/* settings */});
/* --- */
// promise passed to some other block of code
promise.done(function(){
    //execute the block of code after the promise was resolved
});

阅读更多关于这个

PHP文件的响应是:

[
    {"Month":"1","Year":"2015","Sales":"19746.81"},  // 1
    {"Month":"2","Year":"2015","Sale‌​s":"17902.26"},  // 2
    {"Month":"3","Year":"2015","Sales":"19223.84"},  // 3
    {"Month":"4","Year"‌​:"2015","Sales":"18840.88"},  // 4
    {"Month":"5","Year":"2015","Sales":"19889.97"},  // 5
    {"Mont‌​h":"6","Year":"2015","Sales":"18509.85"},  // 6
    {"Month":"7","Year":"2015","Sales":"1886‌​.81"},   // 7
    {"Month":"8","Year":"2015","Sales":"1740.34"}    // 8
]

但是你在循环

for(var i = 0; i <= 11; i++) 

所以当i >= 8, data[i].Month抛出"data[i] is undefined"错误。

直接使用.length属性:

for(var i = 0; i < data.length; i++) 

例子小提琴

修改代码,在循环外声明gloabla_ij变量。

var global_ij='0';
url1 = 'http://localhost:81/Dashboard/admin/inc/dashboard.php';
$.ajax({
    url: url1,
    type: "POST",
    dataType: "json",
    success: function(data)
    {
        for(var i = 0; i <= 11; i++) 
        {
            Month = data[i].Month;
            Sales = data[i].Sales;
            if (i == 0) global_ij = "[" + Month + "," + Sales + "]";
            else global_ij = global_ij + "," + "[" + Month + "," + Sales + "]";
        }
    }
});

如果你在文件的顶部声明var,它可以被文件中的任何函数使用:

var global_ij; // declare variable in global scope of module/file
$.ajax({
    url: 'http://localhost:81/Dashboard/admin/inc/dashboard.php',
    type: "POST",
    dataType: "json",
    success: function(data) {
        var i, month, sales, len = data.length;
        for(i=0; i<=len; i++) {
            month = data[i].Month;
            sales = data[i].Sales;
            if (i===0) global_ij = "[" + month + "," + sales + "]";
            else global_ij = global_ij + "," + "[" + month + "," + sales + "]";
        }
        doSomething(); // can now call function after loop finished
    }
});
func doSomething() {
    // global_ij is now populated
}