473,569 Members | 2,880 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problems with AJAX and global variable

First of all, small example:
function GetURL()
{
new Ajax.Request('/updater.php?Get URL=yes',
{
method:'get',
onSuccess: function(transp ort){
response = transport.respo nseText;
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?Get URL=yes',
{
method:'get',
onSuccess: function(transp ort){
response = transport.respo nseText;
}
});

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 2665
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?Get URL=yes',
{
method:'get',
onSuccess: function(transp ort){
response = transport.respo nseText;
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?Get URL=yes',
{
method:'get',
onSuccess: function(transp ort){
response = transport.respo nseText;
}
});

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
4260
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 variable... Example: /* How do I get rid of this global variable? */ xmlHttp = new XMLHttpRequest();
10
19152
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 am passing to it. Everything works fine on IE 6. Here's a couple samples of what I am doing: // using GET url =...
3
1132
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() { myFunc("MyCodeBehind.MyAjaxProc"); }
10
1957
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 hoping someone can enlighten me. var loader = this; this.req.onreadystatechange = function() { loader.onReadyState.call(loader); }
2
1994
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); resObjekt.onreadystatechange = handleResponse(divID); resObjekt.send(null); }
7
3592
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
11849
by: Arjen | last post by:
Hi, I want to reload 2 divs at one click. Ive tried: <a href = "javascript:void(0);" onclick="show('ajaxrequest.php?action=removefield','div1');show('ajaxrequest.php?action=reloaddiv2','div2')">verwijderen</a> While both seperate actions work they dont when I put them together. Anyone know how to fix this ? My ajax.js with funcition...
2
3139
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 i change something in php file using in ajax function.it not refreshed,means its shows the previous result it not get updated.i can't understand...
0
7619
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7681
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7983
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6290
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5228
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3662
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2118
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.