473,698 Members | 2,643 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Ajax.Request onComplete

69 New Member
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 14485
acoder
16,027 Recognized Expert Moderator MVP
You posted in the Articles section. Moved to Forums section.
Sep 3 '07 #2
acoder
16,027 Recognized Expert Moderator MVP
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 New Member
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 Recognized Expert Expert
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 New Member
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 Recognized Expert Expert
Heya, Sean.

The callback is passed as an anonymous function to called() inside of caller().
Sep 3 '07 #7
acoder
16,027 Recognized Expert Moderator MVP
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
1672
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 I am having a couple of issues when trying to use Prototype. 1. Parameters are not sent if I specify a transport method: var url = baseurl+'lib/login.php'; var pars = 'login=' + $F('ad_login') + '&passwd=' + $F('passwd'); var myAjax = new...
5
20071
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 with Firefox,but with IE6 the onFailure never gets called and the request never completes. My code: var ajaxReq = new Ajax.Request( url, {method: 'post', parameters:
17
3152
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 security settings are not an issue. Anyone have any insight on this? Thanks! -sh
4
7704
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 Ajax.Request( url, {method: 'post', parameters: params, onComplete: evalInfo }); function evalInfo( request ) { // do stuff with request
2
5220
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 webservice and the webservice returns a Dataset that contains various customer details taken from database. I have tested that the Webservice itself works. But my ajax call is not working. My ajax call is something like :
3
3949
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 the bottom of the onComplete function, there is a call to doGetAddressContacts(), which accepts an address ID. This then returns a list of contacts at that address. What I am trying to do is for each address that it retrieves and outputs, as it...
1
1806
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 the other events never fire. I am getting the response because I can see the response from the Mongrel server in my net monitor (Firebug). But the events itself never fire (no oncomplete and or onfailure)
5
2786
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: (connection.method ? connection.method : 'post'), on404: function(request){ alert("File Not Found Exception Occured "); this.connections.waitPop(); this.isProcessing = false; this.processNext(); }.bind(this),
3
2165
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 application which will send an Ajax request and retrieve a link from a website. The particular request is something along the lines of: new Ajax.Request(url, {parameters:pars, onComplete: function(r) { var url = r.responseText; ...
0
9170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8904
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8876
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7741
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4372
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3052
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
2
2341
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.