JavaScript jquery.ajax成功后循环无效

时间:2015-09-21 18:15:02

标签: javascript php jquery ajax

我需要将一些值传递给Javascript中的其他函数。我从另一个PHP文件中获取了值,但我无法将其传递给任何其他Javascript函数。在FOR LOOP之后,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
    }
});

5 个答案:

答案 0 :(得分:1)

那是因为您正在执行异步 AJAX操作。换句话说,您正在进行的变量MonthSaleglobal_ij的分配仅 在该特定success函数中可用&#39 ;范围而不是之外。

一种解决方法是 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调用中获取 promise (您可以在Javascript代码中的任何位置传递)以及当promise 已解决时调用它的.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
});

Read more about this here

答案 1 :(得分:1)

看起来你的php正在返回8个元素,并且在你的成功方法中你的循环迭代超过11个项目,导致错误。

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

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

请注意我添加了

  

var Month;

     

var Sales;

将这些临时变量保留在函数范围内。

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

  

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

最终输出和尝试的东西:

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的函数:

  

过程数据([{&#34;月&#34;:&#34; 1&#34;&#34;年份&#34;:&#34; 2015&#34;&#34;销售&#34 ;:&#34; 19746.81&#34;},
  {&#34;月&#34;:&#34; 2&#34;&#34;年份&#34;:&#34; 2015&#34;&#34;销售&#34;:&#34; 17902.26&#34;},{&#34;月&#34;:&#34; 3&#34;&#34;年份&#34;:&#34; 2015&#34;&#34;销售&# 34;:&#34; 19223.84&#34;},{&#34;月&#34;:&#34; 4&#34;&#34;年份&#34;:&#34; 2015&#34; &#34;销售&#34;:&#34; 18840.88&#34;},{&#34;月&#34;:&#34; 5&#34;&#34;年份&#34;:& #34; 2015&#34;&#34;销售&#34;:&#34; 19889.97&#34;},{&#34;月&#34;:&#34; 6&#34;&#34 ;年份&#34;:&#34; 2015&#34;&#34;销售&#34;:&#34; 18509.85&#34;},{&#34;月&#34;:&#34; 7& #34;&#34;年份&#34;:&#34; 2015&#34;&#34;销售&#34;:&#34; 1886.81&#34;},{&#34;月&#34 ;:&#34; 8&#34;&#34;年份&#34;:&#34; 2015&#34;&#34;销售&#34;:&#34; 1740.34&#34;}]) ;

您可能想要使用.done()

答案 2 :(得分:0)

修改代码以在循环外声明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 + "]";
        }
    }
});

答案 3 :(得分:0)

如果在文件顶部声明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
}

答案 4 :(得分:0)

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会抛出&#34;数据[i]未定义&#34;错误。

只需使用.length属性:

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

Example Fiddle