473,407 Members | 2,359 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,407 software developers and data experts.

Ajax.Request onComplete

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

Expand|Select|Wrap|Line Numbers
  1. function called()
  2. {
  3.       new Ajax.Request( readDatabase.php', {
  4.              method: 'get',
  5.              parameters: 'primaryKey=' + primaryKey,
  6.              onComplete: function( transport ){
  7.  
  8.                  xmlDoc = transport.responseXML;
  9.                  ...
  10.                  ...
  11.              }
  12.        });
  13.  
  14.       alert( "Returning from called function" );
  15. }
  16.  
  17. function caller()
  18. {
  19.       called();
  20.       alert( "Returned to caller" );
  21. }
  22.  
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
Sep 3 '07 #1
7 14438
acoder
16,027 Expert Mod 8TB
You posted in the Articles section. Moved to Forums section.
Sep 3 '07 #2
acoder
16,027 Expert Mod 8TB
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.
Sep 3 '07 #3
Sebarry
69
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.
Sep 3 '07 #4
pbmods
5,821 Expert 4TB
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:
Expand|Select|Wrap|Line Numbers
  1. function called(callback)
  2. {
  3.     new Ajax.Request( readDatabase.php', {
  4.          method: 'get',
  5.          parameters: 'primaryKey=' + primaryKey,
  6.          onComplete: function( transport ){
  7.  
  8.              xmlDoc = transport.responseXML;
  9.              ...
  10.              ...
  11.              callback(xmlDoc);
  12.          }
  13.      });
  14.  
  15.     alert( "Returning from called function" );
  16. }
  17.  
  18. function caller()
  19. {
  20.     called(
  21.         function()
  22.         {
  23.             alert( "Returned to caller" );
  24.         }
  25.     );
  26. }
  27.  
Sep 3 '07 #5
Sebarry
69
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:
Expand|Select|Wrap|Line Numbers
  1. function called(callback)
  2. {
  3.     new Ajax.Request( readDatabase.php', {
  4.          method: 'get',
  5.          parameters: 'primaryKey=' + primaryKey,
  6.          onComplete: function( transport ){
  7.  
  8.              xmlDoc = transport.responseXML;
  9.              ...
  10.              ...
  11.              callback(xmlDoc);
  12.          }
  13.      });
  14.  
  15.     alert( "Returning from called function" );
  16. }
  17.  
  18. function caller()
  19. {
  20.     called(
  21.         function()
  22.         {
  23.             alert( "Returned to caller" );
  24.         }
  25.     );
  26. }
  27.  
Sep 3 '07 #6
pbmods
5,821 Expert 4TB
Heya, Sean.

The callback is passed as an anonymous function to called() inside of caller().
Sep 3 '07 #7
acoder
16,027 Expert Mod 8TB
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.
Sep 4 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
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...
5
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...
17
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...
4
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...
2
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...
3
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...
1
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...
5
RamananKalirajan
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:...
3
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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
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,...
0
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...

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.