By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,607 Members | 1,982 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,607 IT Pros & Developers. It's quick & easy.

Problems with AJAX and global variable

P: n/a
First of all, small example:
function GetURL()
{
new Ajax.Request('/updater.php?GetURL=yes',
{
method:'get',
onSuccess: function(transport){
response = transport.responseText;
alert(response);
}
});

}

PHP script echo URL, AJAX gets it and response should be and is
"http://localhost/forum". So far everything is okay, but I would like
GetURL() return that response and I can't get it out from new
Ajax.Request. Let's say I would write like this:

function GetURL()
{
var response;

new Ajax.Request('/updater.php?GetURL=yes',
{
method:'get',
onSuccess: function(transport){
response = transport.responseText;
}
});

return response;

}

But it won't happen ever. response will be set as unidentified. Any
ideas how I could make GetURL() to return what I need?

P.S. Thanks for your all help.

Jun 23 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
david wrote:
First of all, small example:
First of all, you should mention the library you are using. :-)

function GetURL()
{
new Ajax.Request('/updater.php?GetURL=yes',
{
method:'get',
onSuccess: function(transport){
response = transport.responseText;
alert(response);
}
});

}

PHP script echo URL, AJAX gets it and response should be and is
"http://localhost/forum". So far everything is okay, but I would like
GetURL() return that response and I can't get it out from new
Ajax.Request. Let's say I would write like this:

function GetURL()
{
var response;

new Ajax.Request('/updater.php?GetURL=yes',
{
method:'get',
onSuccess: function(transport){
response = transport.responseText;
}
});

return response;
Your request is running asynchronously. When GetURL executes this line,
the onSuccess function hasn't set the value of response yet. When it
does, GetURL has finished executing.

>
}

But it won't happen ever. response will be set as unidentified. Any
ideas how I could make GetURL() to return what I need?
Make it a synchronous call, but then what's the point of AJAX?
--
Rob
"We shall not cease from exploration, and the end of all our
exploring will be to arrive where we started and know the
place for the first time." -- T. S. Eliot
Jun 24 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.