Problems with AJAX and global variable

D

david

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.
 
R

RobG

david said:
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top