943,670 Members | Top Members by Rank

Ad:
Sep 2nd, 2009
0

Ajax miss behaving

Expand Post »
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!!!!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Cheeki is offline Offline
19 posts
since May 2009
Sep 2nd, 2009
0

Re: Ajax miss behaving

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.
Reputation Points: 83
Solved Threads: 61
Posting Pro in Training
Luckychap is offline Offline
442 posts
since Aug 2006
Sep 2nd, 2009
0

Re: Ajax miss behaving

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??
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Cheeki is offline Offline
19 posts
since May 2009
Sep 3rd, 2009
0

Re: Ajax miss behaving

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.
Reputation Points: 83
Solved Threads: 61
Posting Pro in Training
Luckychap is offline Offline
442 posts
since Aug 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: sound problem
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Embid php to javascript





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC