aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes inconsistent  ajax calls to servlet functions 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 "inconsistent  ajax calls to servlet functions" Watch "inconsistent  ajax calls to servlet functions" New topic
Author

inconsistent ajax calls to servlet functions

Peter Brown
Greenhorn

Joined: Sep 26, 2008
Posts: 22
Hello,

I realise that ajax is asynchronous but I hope this is not the reason for my issue.

I am using the MVC model. I have an order page. Top right has tally section (no of lines, no of pieces & subtotal). I use ajax to update this - works without issue.

When an user changes a quantity the following event fires two javascript functions - updOrder() & updExtend():



updOrder() updates the tally section. updExtend() updates the extended price.

When it it working the console shows:



So addOrdItem is called from updOrder() which makes the item quantity & price changes and responds with an xml style respone to update the tally section. As I said - no issue.

Then ordItemExtend is called from updExtend(). (I list the contents of the array to make sure everything matches) ordItemExtend returns the extended price and I update the extended field using the DOM model. I get no error messages at the server or browser level (use Firefox error console) when it works or does not work completely.

When it doesn't work - this is what I get:



The last two lines should be right after "addOrdItem orditem is: 550586 1
" but are not and so the first line of the ordItemExtend check has the wrong quantity and extended price.

I suspected the browser cache so tried the ServletFAQ no-cache solution and sticking a random number on the end of the URL. The response.setHeader no-cache etc did not work (or I could not get it to fix the problem). The random number worked but refreshes the whole page and I lose my tab order.

Puzzled why the updOrder() tally total works consistently and the updExtend() does not. I have tried separating them - putting updOrder() onchange and updExtend() onblur with no success.

Please advise. Thanks.
Peter Brown
Greenhorn

Joined: Sep 26, 2008
Posts: 22
Update - Firefox is affected by it. Internet Explorer is updating extended pricing without issue... I will see what I can find to fix this and close the thread - unless someone beats me to it.

Thanks.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

Originally posted by Pedro Bruno:
Hello,

I realise that ajax is asynchronous but I hope this is not the reason for my issue.



It very well might be the reason. Without actually seeing the code you write to make the ajax calls it is hard to say. I suspect that it is because you are issues the calls back to back. The problem with that is the second ajax call might actually complete before the first one, depending on a whole lot of factors like network latency, database latency, whatever. If you need to ensure that one call happens before/after another call then you need to code it that way.

I don't know if you are using a javascript library or doing the ajax the old school way but you should have some sort of callback method for when the ajax calls complete. Your updExtend() call should be placed in this on complete callback function for updUpdate(). That way you know for sure the updUpdate() is going to be called and complete before the updExtend() call ever fires.


GenRocket - Experts at Building Test Data
Peter Brown
Greenhorn

Joined: Sep 26, 2008
Posts: 22
Thanks Gregg.

I will try that as I would like to see it work in Firefox so I can offer both options to end users.
Peter Brown
Greenhorn

Joined: Sep 26, 2008
Posts: 22
Now I remember why I didn't try this - got confused...

My updOrder code is



The validateCallback is



If I understand correctly I should be putting the updExtend() inside the "msg == updated" statement. My question is what is the format for passing parameters into validateCallback?

updExtend() has three fields it requires.

Do I do something like this in the updOrder() function?


and if so does my validateCallback look like this:



Which does not seem to make sense to me...

Thanks,
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
You need a closure

var ajax = new AJAXInteraction(url, function(response){ validateCallback( response, field1, field2, field3); });

Eric
Peter Brown
Greenhorn

Joined: Sep 26, 2008
Posts: 22
Thanks for the correction Eric.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: inconsistent ajax calls to servlet functions