admin管理员组文章数量:1333186
I am using Ajax with Prototype library.
Here is my function that calls the Ajax function.
function Testfn()
{
var DateExists = '';
new Ajax.Request('testurl',{
method: 'post',
parameters: {param1:"A", param2:"B", param3:"C"},
onSuccess: function(response){
//DateExists = response.responseText;
DateExists = 1;
}
});
// I want to access the value set in the onsuccess function here
alert(DateExists);
}
When i alert the DateExists value i am getting null value instead of the value that is set in the onsuccess function of my Ajax call which is 1. How is that possible?
Thanks for any help.
I am using Ajax with Prototype library.
Here is my function that calls the Ajax function.
function Testfn()
{
var DateExists = '';
new Ajax.Request('testurl',{
method: 'post',
parameters: {param1:"A", param2:"B", param3:"C"},
onSuccess: function(response){
//DateExists = response.responseText;
DateExists = 1;
}
});
// I want to access the value set in the onsuccess function here
alert(DateExists);
}
When i alert the DateExists value i am getting null value instead of the value that is set in the onsuccess function of my Ajax call which is 1. How is that possible?
Thanks for any help.
Share Improve this question edited Dec 28, 2011 at 21:52 Rob W 349k87 gold badges807 silver badges682 bronze badges asked Aug 7, 2009 at 5:51 ajithmanmuajithmanmu 1,1694 gold badges13 silver badges16 bronze badges3 Answers
Reset to default 3The A in AJAX stands for Asynchronous. This means that as soon as you dispatch that Ajax request using new Ajax.Request
the request is sent to the server and immediately returns control to your script. Thus, alert(DateExists) will show '' which you initially set.
To see the value of DateExists after returning from the AJAX request, you must move it inside the onSuccess() method.
Example:
function Testfn() {
var DateExists = '';
new Ajax.Request('testurl', {
method: 'post',
parameters: {param1:"A", param2:"B", param3:"C"},
onSuccess: function(response){
DateExists = response.responseText;
alert(DateExists);
}
});
}
The onSuccess callback is executed asynchronously, when the AJAX request ends, so the alert is firing before the callback is called.
You should work with your response, inside the callback or if you want, make another function:
new Ajax.Request('testurl',{
method: 'post',
parameters: {param1:"A", param2:"B", param3:"C"},
onSuccess: function(response){
var dateExists = response.responseText;
doWork(dateExists);
// or alert(dateExists);
}
});
function doWork (data) {
alert(data);
}
CMS is exactly right. The solution is to call the javascript that needs access to DateExists from within the AJAX callback, like this:
function Testfn()
{
var DateExists = '';
new Ajax.Request('testurl',{
method: 'post',
parameters: {param1:"A", param2:"B", param3:"C"},
onSuccess: function(response){
//DateExists = response.responseText;
DateExists = 1;
doTheRestOfMyStuff(DateExists);
}
});
// I want to access the value set in the onsuccess function here
function doTheRestOfMyStuff(DateExists)
{
alert(DateExists);
}
}
本文标签: javascriptAccessing value set inside onSuccess function in PrototypeStack Overflow
版权声明:本文标题:javascript - Accessing value set inside onSuccess function in Prototype - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742276126a2445230.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论