File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes Problem with Synchronous Ajax call Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Problem with Synchronous Ajax call" Watch "Problem with Synchronous Ajax call" New topic
Author

Problem with Synchronous Ajax call

Naresh Talluri
Ranch Hand

Joined: Oct 12, 2007
Posts: 115
Hi folks

I have a small problem with making synchronous Ajax call,

Scenario:

1) I have to hit server by making ajax call in for loop, am having 5-10 values in Array to iterate,
for each iterate am placing constructing some html and placing in variable with respect to request,
2) Here problem is it is overriting all values with last value.
3) I thought of making it sync ajax call to wait until this request is processed , but with synchronous call it is not going to ready state change method.

code snippet :



Could you please let me know where it went wrong?
Really appriciate if any ideas..



Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

How can you expect us to help you with a code snippet that has nothing to do with the problem you described.

1) You say you have a for loop, but you didn't know us the for loop. Without that context of understanding we cant help.
2) You say it is overriding values but you just have a comment in the place where the work is being done. No code == no way we can help
3) Again, not enough code to determine what your problem might be.


GenRocket - Experts at Building Test Data
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
It is easy, you have a GLOBAL variable. You are looping over an array. You keep overriding the GLOBAL variable.

1) You need to make a Queue system
or
2) Use an Ajax call that does not rely on global variables. [Most libraries such as jQuery will make this easier]

Eric
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Eric, I'm glad your crystal ball is working today. Mine is on the fritz. ;)
Naresh Talluri
Ranch Hand

Joined: Oct 12, 2007
Posts: 115
Hi

Here is the code snippet:





I found by googling Ajax sync request is won't work in FireFox, am using Firefox browser.

Thank you for your information.>
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42264
    
  64
So all you need to do is to wait for a request to finish before sending the next one.


Ping & DNS - my free Android networking tools app
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Well you are using a local variable so the call problem appears to be a different issue.



That has a global variable problem, it needs a var



You need to do some debugging to figure out where the values are being overwritten.

Firebug and console.log will be your friend.

Eric>
Naresh Talluri
Ranch Hand

Joined: Oct 12, 2007
Posts: 115
Hi

I got the problem with Synchrnous ajax call, for these kind of request we should not use response handler ( like onreadystatechange handler ), for async calls we have to take response once we give request immediately ( by using req.responseText() ).

Thank you for your help.
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

here is what i understand from your problem:

The doAjaxCall is executed many times in a loop,
Mostly you would expect that when the doAjaxCall function completes, the next iteration is made,

However, you may be wrong in one case...
the code:



indicates that the Business logic code will execute in 'its own sweet time'
there is no guarantee of order here,

AJAX calls are inherently asynchronous, which means that supposed your business logic printed "hi" in one execution and "bye" in another,
in the example you provided, there is no guarantee where "hi" would be printed first or "bye"


As Ulf Dittmer rightly stated, you need to wait for a request to finish


Here is a simple algo for that:

main program:



function test:



Hope this makes sense


My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with Synchronous Ajax call