Ajax miss behaving

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: May 2009
Posts: 19
Reputation: Cheeki is an unknown quantity at this point 
Solved Threads: 0
Cheeki Cheeki is offline Offline
Newbie Poster

Ajax miss behaving

 
0
  #1
Sep 2nd, 2009
Hey guys,

Im having the weirdest problems with my ajax. Now first off i have horrible coding convention!! I have one Ajax.js file which handles all my ajax requests and one ajax.php file which handles retrieving data from a database.
The problem I am having is when ever I call a certain method which contains a if statement and runs the respective ajax function, it sometimes evaluates the if statement wrong. The weirdest thing of all is when I use FireFox's firebug and step through the javascript code to see why its evaluating the if statement wrong, it evaluates it correctly.

So basically if I don't step through the javascript code it evaluates the if statement wrong which means one of my variables are not being set in time for it to be used in the if statement. BUT if i step through the code with firebug it evaluates the if statement correctly!!!!!!

Now I have tried this is all the browsers and sometimes it evaluates the if statement correctly and when i keep refreshing the page it will sometimes evaluate the if statement correctly!!!!

Now I know this makes no sense, but I'm just as confused! The only reason I can think why this is happening is that one of my variables are not being set in time for the if statement to use it!!

Can anyone help me pleeeeaaassee!!! ITS DRIVING ME INSANE!!!!
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 319
Reputation: Luckychap is on a distinguished road 
Solved Threads: 42
Luckychap's Avatar
Luckychap Luckychap is offline Offline
Posting Whiz

Re: Ajax miss behaving

 
0
  #2
Sep 2nd, 2009
As you know Ajax is asynchronous, so your thinking is correct. The variable is set synchronously and respond is asynchronous(ajax). Check your code where the ajax request has completed for that variable.
When you think you have done a lot, then be ready for YOUR downfall.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 19
Reputation: Cheeki is an unknown quantity at this point 
Solved Threads: 0
Cheeki Cheeki is offline Offline
Newbie Poster

Re: Ajax miss behaving

 
0
  #3
Sep 2nd, 2009
I was thinking it would be a problem along those lines. Now I really don't know Ajax 100%, I know its basic functionalities.

Now below is the code which I think is causing the problems. Basically whats happening is that I go to a php method which calculates the total size of buttons and other values and returns these values. Then as you can see I call a method:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. HandleResponseFlowButtonSize(xmlHttp.responseText);
which passes the "response" from the php method and breaks the response up accordingly and sets the values to different variables.

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function getbuttonSize(id)
  2. {
  3. checkNumofContainers();
  4.  
  5. var xmlHttp = getXMLHttp();
  6.  
  7. xmlHttp.onreadystatechange = function()
  8. {
  9. if(xmlHttp.readyState == 4)
  10. {
  11. HandleResponseFlowButtonSize(
  12. xmlHttp.responseText);
  13. }
  14. }
  15.  
  16. xmlHttp.open("GET", '../ajax/ajax.php?action=checkNumberofButtons&value='+id, true);
  17. xmlHttp.send(null);
  18. }
  19.  
  20. function HandleResponseFlowButtonSize(response)
  21. {
  22. var reply = response;
  23. var displayButtos = new Array(500);
  24.  
  25. var getReply = reply.split(",");
  26.  
  27. for(i = 0; i < 1; i++)
  28. {
  29. buttonSize = getReply[i];
  30. displayButtonNum = getReply[i+1];
  31. numofButtons = getReply[i+2];
  32. }
  33. buttonSize = Number(buttonSize);
  34. displayButtonNum = Number(displayButtonNum);
  35. numofButtons = Number(numofButtons);
  36. }

Do you maybe have a different or should I say better way of doing this??
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 319
Reputation: Luckychap is on a distinguished road 
Solved Threads: 42
Luckychap's Avatar
Luckychap Luckychap is offline Offline
Posting Whiz

Re: Ajax miss behaving

 
0
  #4
Sep 3rd, 2009
This code seems absolutely fine. But you need to check if function 'HandleResponseFlowButtonSize(response)' is called only once.
And also check if function 'getbuttonSize(id)' is called once for every request.

If any of the above are called twice then there is something wrong else where.

One thing more Ajax is browser dependent so use some good libraries like (YUI) http://developer.yahoo.com/yui/connection/

This will simplify your code to great extend and will take care of browser compatibilities.
When you think you have done a lot, then be ready for YOUR downfall.
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC