473,408 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Problems with AJAX and global variable

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
1 2652
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: napawlm | last post by:
Is there a way to "pass" an XMLHttpRequest object to the callback function for onreadystatechange? Or a way to access it from onreadystatechange? I would like to avoid the use of a global...
10
by: Danny | last post by:
Hi all, I am having some odd problems with AJAX on Firefox (1.5). When I use GET as the request method everything works ok, but when I do a POST the remote function doesn't get the parameters I...
3
by: Hoss | last post by:
Hello all- The best way to describe what im trying to accomplish is with a code snippet. Im using the free .NET ajax library. <script language='javascript'> function proc1() {...
10
by: lkagan2000 | last post by:
I'm reading 'Ajax in Action', (one of the best of the many tech books I've read) and there's an example that I just don't fully understand. If you happen to have the book, it's on page 75. I'm...
2
by: Mark Knochen | last post by:
Hi, i have a little problem with ajax. The follow functions are in my site: function sndReq(ID,divID) { resObjekt.open('get','inc/inc_change_pagelogo.php?ID='+ID,true);...
7
ak1dnar
by: ak1dnar | last post by:
Hi, I got this scripts from this URL There is Error when i submit the form. Line: 54 Error: 'document.getElementbyID(....)' is null or not an object What is this error. Complete Files
17
by: Arjen | last post by:
Hi, I want to reload 2 divs at one click. Ive tried: <a href = "javascript:void(0);"...
2
by: shivendravikramsingh | last post by:
hi friends, i m using a ajax function for retrieving some values from a database table,and display the values in required field,my prob is that the ajax function i m using is working f9 once,but if...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.