JavaScript数组调试,范围母校 [英] Javascript array debugging, a mater of scope
问题描述
<一个href=\"http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call\">How做回我从异步调用的响应?
没有帮我,可能会迷惑很多Google员工因为
它的状态是什么工作作为不工作。女巫; the_data_you_want
=响应,这其实就是答案。
块引用>我的工作是使用Ajax,Jquery的,无极,和PHP的一个项目。
我填一个函数的数组。
VAR数据= validateInput2(FIELD_ID,DATA_TYPE,值)。然后(功能(响应){
的console.log(更迭!,响应); &LT; -----------------------------
数据=响应;范围,其中数据被填充
警报('R:'+数据); &LT; -----------------------------
},功能(错误){
console.error(Échec!,错误);
});
警报('数据:'+数据); &LT; ---------------------------
如果(数据[0] =='真'){以外的范围,其中数据
$(容器[1])addClass(pass_f);是一个悬而未决的承诺对象
$(容器[1])的html(数据[2]);
$(容器[0])的html(数据[1]);
}
其他{
$(容器[1])removeClass移除(pass_f);
$(容器[1])addClass(err_f);
$(容器[1])的html(数据[2]);
$(容器[0])的html(数据[1]);
}现在,如果你看看警报声明,第一个
警报('R:'+数据);
是显示我什么,我想看到的。数组从PHP送回来。
我可以看到,因为的console.log(更迭!,响应)控制台姣好值;
但是当我继续下去,并获得了部分所在的页面应该得到更新。女巫是警报('数据:'+数据);
的数据不再是什么。相反,我得到[对象的对象]
在警告框。并在控制台中我得到
无极{[PromiseStatus]:待定,[PromiseValue]:未定义}
块引用>这似乎是包含在结果数组是唯一没有在里面;
{
的console.log(更迭!,响应);
数据=响应;
警报('R:'+数据);
}所以我知道我的承诺是工作,因为我的网页正在等待与未决状态对象,我不再有数据未定义的错误。
解决方案VAR数据= validateInput2(FIELD_ID,DATA_TYPE,值)。然后(功能(响应){
的console.log(更迭!,响应);
数据=响应;
如果(数据[0] =='真'){
$(容器[1])addClass(pass_f);
$(容器[1])的html(数据[2]);
$(容器[0])的html(数据[1]);
}
其他{
$(容器[1])removeClass移除(pass_f);
$(容器[1])addClass(err_f);
$(容器[1])的html(数据[2]);
$(容器[0])的html(数据[1]);
}
},功能(错误){
console.error(Échec!,错误);
});
警报('数据:'+数据);那是因为你正在使用数据,并在那一刻数据的承诺,所以像这样做
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.
I am working on a project that uses Ajax, Jquery, Promise, and PHP.
I fill an array with a function.
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]); }
Now if you take a look at the alert statement, The first one
alert('R : '+data);
is showing me what i am suppose to see. an array sent back from PHP. I can see the same good values in the console because ofconsole.log("Succès !", response);
but when i keep going and get to the part where the page should get updated. Witch is thealert('DATA : '+data);
The data is no longer what it was. Instead I get[object Object]
in the alert box. And in the console I getPromise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}
It seems like the array contained in result is only there inside;
{ console.log("Succès !", response); data = response; alert('R : '+data); }
So I know that my Promise is working because my page is waiting for the object with the status pending and that I no longer have the data undefined error.
解决方案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);
it's because you are working with data, and data in that moment is promise, so do it like this
这篇关于JavaScript数组调试,范围母校的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!