【问题标题】:Javascript array debugging, a mater of scopeJavascript数组调试,范围问题
【发布时间】:2016-04-24 14:33:20
【问题描述】:

How do I return the response from an asynchronous call? 没有帮助我,可能会让谷歌用户感到困惑,因为 它的状态是什么工作不工作。女巫是; the_data_you_want = 响应,这实际上就是答案。

我正在开发一个使用 Ajax、Jquery、Promise 和 PHP 的项目。

我用一个函数填充一个数组。

var data = validateInput2(field_id, data_type, value).then(function (response) {
        console.log("Succès !", response);  <-----------------------------
        data = response;                     Scope where data is filled
        alert('R : '+data);                 <-----------------------------
    }, function (error) {
        console.error("Échec !", error);
    });
    alert('DATA : '+data);                    <---------------------------
    if (data[0] == 'true') {                   Outside the scope where data 
        $(container[1]).addClass("pass_f");    is a pending promise object
        $(container[1]).html(data[2]);
        $(container[0]).html(data[1]);
    }
    else {
        $(container[1]).removeClass("pass_f");
        $(container[1]).addClass("err_f");
        $(container[1]).html(data[2]);
        $(container[0]).html(data[1]);
    }

现在,如果您查看警报声明,第一个 alert('R : '+data); 正在向我展示我应该看到的内容。从 PHP 发回的数组。 由于console.log("Succès !", response);,我可以在控制台中看到相同的好值,但是当我继续前进并到达页面应该更新的部分时。女巫是alert('DATA : '+data); 数据不再是原来的样子。相反,我在警告框中得到[object Object]。在控制台中我得到 ​​p>

承诺{[[PromiseStatus]]:“待定”,[[PromiseValue]]:未定义}

结果中包含的数组似乎只在里面;

  {
        console.log("Succès !", response);
        data = response;                  
        alert('R : '+data);
    }

所以我知道我的 Promise 正在工作,因为我的页面正在等待状态为 pending 的对象,并且我不再有 data undefined 错误。

【问题讨论】:

  • 变量data 是一个局部变量,您正试图在其范围之外访问它
  • 所以如果我将所有代码移到那里它应该可以工作吗?
  • 你为什么要用response覆盖var data
  • console.log("Succès !", response) 将使用浏览器对这两个参数的特殊显示:response 作为一个对象被渲染到漂亮的下拉表格中。用加号替换逗号,你会得到非常不同的东西:'DATA : '+data 将调用data.toString() 以便能够执行连接,并且对象的默认toString[object Object]console.log 然后输出它给出的字符串:"R : [object Object]". Your data` 变量在那一刻仍然正确。接受的答案中提到的超出范围的问题是不相关的。

标签: javascript jquery arrays ajax promise


【解决方案1】:

为了清楚起见,我已展开您的回调函数,以便您发现错误:

var success_func = function (response) {
    console.log("Succès !", response);
    data = response;
    alert('R : '+data);
};
var error_func = function (error) {
    console.error("Échec !", error);
};
var data = validateInput2(field_id, data_type, value).then(success_func, error_func);
alert('DATA : '+data);
if (data[0] == 'true') {
    // ...
} else {
    // ...
}

success_func 会在将来某个时间调用,当您的 ajax 调用完成时。所以当validateInput2 返回时,很可能success_func 甚至还没有被调用。 then 返回一个 Promise 对象,而不是您期望的来自服务器的数据。

处理这种情况的正确方法是将所有数据处理放入success_func

validateInput2(field_id, data_type, value).then(function (response) {
    console.log("Succès !", response);
    data = response;
    if (data[0] == 'true') {
        $(container[1]).addClass("pass_f");
        $(container[1]).html(data[2]);
        $(container[0]).html(data[1]);
    }
    else {
        $(container[1]).removeClass("pass_f");
        $(container[1]).addClass("err_f");
        $(container[1]).html(data[2]);
        $(container[0]).html(data[1]);
    }
}, function (error) {
    console.error("Échec !", error);
});

【讨论】:

  • 谢谢。确实解决了它。我花了很多时间在这上面,因为 promise 对我来说是新的。但至少现在我明白了。能够做到这一点真是太棒了!
【解决方案2】:
var data = validateInput2(field_id, data_type, value).then(function (response) {
        console.log("Succès !", response);
        data = response;
        if (data[0] == 'true') {
            $(container[1]).addClass("pass_f");
            $(container[1]).html(data[2]);
            $(container[0]).html(data[1]);
        }
        else {
            $(container[1]).removeClass("pass_f");
            $(container[1]).addClass("err_f");
            $(container[1]).html(data[2]);
            $(container[0]).html(data[1]);
        }
    }, function (error) {
        console.error("Échec !", error);
    });
    alert('DATA : '+data);

因为你在处理数据,而那一刻的数据是有希望的,所以就这样吧

【讨论】:

  • YESSSSS 我刚刚做了,回到这里,看到你想出了同样的代码。 6小时后非常感谢你,我终于明白了!感谢上帝,我不需要重新发明轮子
猜你喜欢
  • 2013-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-13
  • 2013-02-25
相关资源
最近更新 更多