Hi,
I have a Javascript function that creates an Ajax Request object in order to retrieve data from a database using a PHP script. The Javascript function is invoked by another Javascript function and should return to the caller only once the request is complete. Currently the function that creates the Ajax.Request object returns to the caller then initiates the request.
Maybe it's easier to understand with a code sample. -
function called()
-
{
-
new Ajax.Request( readDatabase.php', {
-
method: 'get',
-
parameters: 'primaryKey=' + primaryKey,
-
onComplete: function( transport ){
-
-
xmlDoc = transport.responseXML;
-
...
-
...
-
}
-
});
-
-
alert( "Returning from called function" );
-
}
-
-
function caller()
-
{
-
called();
-
alert( "Returned to caller" );
-
}
-
Whilst onComplete ensures that the code that processes data returned from the data must wait I really want the called function to only return to the caller function when it is complete. The caller function carries out further processing on the database data and can only do that when the called function completes the Ajax Request.
Hope that's clear.
Thanks,
Sean
7 14438
You posted in the Articles section. Moved to Forums section.
Either put that functionality into the function that you call onComplete, or use a synchronous (as opposed to an asynchronous) request. The first option is obviously better because the second option would freeze the browser until the processing is complete.
Hi,
Yes I was thinking it would have to be synchronous because the code that it goes off to execute (whilst its waiting) is in another function for a reason. I don't to duplicate or move the code there because the function is invoked independently of the function with the Ajax.Request.
Cheers,
Sean
Either put that functionality into the function that you call onComplete, or use a synchronous (as opposed to an asynchronous) request. The first option is obviously better because the second option would freeze the browser until the processing is complete.
Heya, Sean.
In this situation, it looks like you'll want to make your XMLHttpRequest synchronous.
You could do this, though outside of the context of your example, I don't know if it would work: -
function called(callback)
-
{
-
new Ajax.Request( readDatabase.php', {
-
method: 'get',
-
parameters: 'primaryKey=' + primaryKey,
-
onComplete: function( transport ){
-
-
xmlDoc = transport.responseXML;
-
...
-
...
-
callback(xmlDoc);
-
}
-
});
-
-
alert( "Returning from called function" );
-
}
-
-
function caller()
-
{
-
called(
-
function()
-
{
-
alert( "Returned to caller" );
-
}
-
);
-
}
-
Hi, I think you're right. I've coded around it at the moment, but it's a bit of a fudge. In your code sample what is the callback function you refer to? I tried setting options in the Ajax.Request to asynchronous = false but that doesn't seem to work. Also people have said that synchronous mode doesn't work in Firefox and can cause browsers to hang indefinately.
Heya, Sean.
In this situation, it looks like you'll want to make your XMLHttpRequest synchronous.
You could do this, though outside of the context of your example, I don't know if it would work: -
function called(callback)
-
{
-
new Ajax.Request( readDatabase.php', {
-
method: 'get',
-
parameters: 'primaryKey=' + primaryKey,
-
onComplete: function( transport ){
-
-
xmlDoc = transport.responseXML;
-
...
-
...
-
callback(xmlDoc);
-
}
-
});
-
-
alert( "Returning from called function" );
-
}
-
-
function caller()
-
{
-
called(
-
function()
-
{
-
alert( "Returned to caller" );
-
}
-
);
-
}
-
Heya, Sean.
The callback is passed as an anonymous function to called() inside of caller().
I tried setting options in the Ajax.Request to asynchronous = false but that doesn't seem to work. Also people have said that synchronous mode doesn't work in Firefox and can cause browsers to hang indefinately.
Ajax.Request must be a library function call or something. What are you using for Ajax? As for the synchronous mode causing problems, that can obviously happen. If it is likely to happen, don't use Ajax. You could also consider using a progress image or show a div indicating to the user that something is happening.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: morganwhitney |
last post by:
Hi all, I am developing a web app and I am implementing the JavaScript
using the Prototype Framework. I have done all the same stuff from
scratch in a previous application and it worked fine, but...
|
by: dougwig |
last post by:
I'm trying to handle the scenario where a user's session times out and
and their ajax request triggers a redirection by the webserver (302
error?).
I'm using Prototype 1.4 and the my works great...
|
by: Steve-O |
last post by:
The following code works great in FireFox, Opera, Netscape, Safari, and
Gecko, but NOT IE. Why?
I tried using 'native' js with setInterval and setTimeout, but I get
the same result. My IE...
|
by: ext237 |
last post by:
Simple ajax call seems to have some issues in Firefox. The "onComplete:" is
called BEFORE the response is returned by the call. Is there a coding issue
or a work around?
var ajax = new...
|
by: Zeba |
last post by:
Hi guys!
I'm new to JS / Ajax; I've been trying to do an Ajax call to my
Webservice ( I'm using C# for code-behind). I'm not using any of the
libraries available. I am sending my CustID to the...
|
by: dhsieh |
last post by:
I am trying out nested AJAX calls for the first time, but I seem to have hit a snag. The code snippet is the outer AJAX call and a function, it gathers information about a company.
As we get towards...
|
by: maildmz |
last post by:
Goodafternoon,
I have got a minimum Mongrel instance running (see ruby code) and i am
hitting it with an Ajax request using Prototype. (see javascript code)
I get the 'onLoading' event, but...
|
by: RamananKalirajan |
last post by:
Hi guys, I am having a problem in Prototypejs AJAX, I am triggering the AJAX call and in the option i am using like the folowing code:
new Ajax.Request(connection.url, {
method:...
|
by: IanWright |
last post by:
I'm just wondering if anyone has used Ajax at all in C# without the use of ASP and if anyone could give me a pointer on getting started?
What I'm trying to do is quite simple, extend a small...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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...
|
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,...
|
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...
| |