gpt4 book ai didi

Javascript数组调试,范围问题

转载 作者:行者123 更新时间:2023-11-30 09:54:08 25 4
gpt4 key购买 nike

How do I return the response from an asynchronous call? Did not helped me and will probably confuse a lot on googlers because its state what is working as not working. witch is; the_data_you_want = response and this is actually the answer.

我正在从事一个使用 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]。在控制台中,我得到了

Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}

result中包含的数组好像只在里面;

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

所以我知道我的 Promise 正在运行,因为我的页面正在等待状态为 pending 的对象,并且我不再有数据未定义错误。

最佳答案

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);

那是因为你在和数据打交道,那一刻的数据是有希望的,所以就这样做吧

关于Javascript数组调试,范围问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34871371/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com