By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,462 Members | 3,388 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,462 IT Pros & Developers. It's quick & easy.

Ajax responses very slow.

P: 22
Hi Friends,

I have a web application through which, the ajax is used to post the data onto the server and return the results. When i alert the xmlhttp.readystate sometimes it takes long time, that the process looks like freezing and sometimes its done very quickly as it is designed to.

I checked by alerting xmlhttp.readystate.

sometimes i get response 1,2 ,3 4 as the alert box quickly.
and sometimes i just get 1 and then nothing happens.

Has any one experienced the same.

Regards,
Anubhav
Aug 24 '09 #1
Share this Question
Share on Google+
10 Replies


dlite922
Expert 100+
P: 1,584
maybe problem with your PHP file. Try to send the same variables to the file through a manual call (enter it in your browser address bar), it maybe that this file never gets back.

Only thing I can think of that causes this.



Dan
Aug 24 '09 #2

Dheeraj Joshi
Expert 100+
P: 1,123
Just try to get the response from a dummy PHP file...

If the response time is same for every call.. then as dlite922 told its a problem with your PHP... Prpbably you need to fix some issues..

Regards
Dheeraj Joshi

I did not faced such problems.
Aug 25 '09 #3

P: 22
I checked that , but if i send the variables directly to the php file as the action in the form tag, it goes out fine. I am using IE 7.0 and sometimes it works fine and sometimes does not work.
Aug 25 '09 #4

Dheeraj Joshi
Expert 100+
P: 1,123
Check it in FF(Fire Fox)... I got some problem when i used AJAX call in IE(Though not similar)...

Regards
Dheeraj Joshi
Aug 25 '09 #5

P: 22
Hi Dheeraj,, even firefox and ie shows the same response. and there is a javascript code written that the web application cannot be used on a firefox. can you think of something else.
Aug 26 '09 #6

dgreenhouse
Expert 100+
P: 250
Do think that multiple requests are being fired off?

If so, that may part of the bottleneck.

If I remember correctly, IE will open only 2 connections at once.

You might also want to add a timeout in your Ajax callback function so the application is more responsive.

http://ajax.sys-con.com/node/352578

Post your httpRequest and callback function.
Aug 26 '09 #7

gits
Expert Mod 5K+
P: 5,333
that seems more to be a problem with the JavaScript-code then with the php-code. could you post the JavaScript-code that is relevant for the ajax-call?

kind regards
Aug 26 '09 #8

Dheeraj Joshi
Expert 100+
P: 1,123
Ok.... I dont know any other solution.

Just post the Java script like GITS told... It may be the culprit....

Regards
Dheeraj Joshi
Aug 26 '09 #9

P: 22
@dgreenhouse
Thank you for your reply.

I dont know to check if the multiple requests are being fired off.can you pls provide detail on it.
Aug 27 '09 #10

P: 22
First i was using the following codes:-

The function is called on submit of a button.

Expand|Select|Wrap|Line Numbers
  1. function submitform()
  2. {
  3. result=0;
  4. //gettting the form values;
  5. var requestid=document.getElementById("requestid").value;
  6. var sdenggname=document.getElementById("sdenggname").value;
  7. var sla=document.getElementById("sla").value;
  8. var helpdesk_comments=document.getElementById("helpdesk_comments").value;
  9. var calltype=document.getElementById("calltype").value;
  10. //validation part
  11. if(sdenggname=="--"||sla=="--"||helpdesk_comments==""||calltype=="--")
  12. {
  13. alert("Please make sure that you have filled in all the fields");
  14. return false;
  15. }
  16. //validateion part end
  17. var domain=document.domain;
  18.  
  19. var url = "http://"+domain+"/crfportal/assignhelpdeskcalls_submit.php"; //url
  20.  
  21. var params = "requestid="+requestid+"&sdenggname="+sdenggname+"&sla="+sla+"&helpdesk_comments="+helpdesk_comments+"&calltype="+calltype; //generating parameters
  22.  
  23. //displaying the user with a statment while the request is being sent.
  24. document.getElementById("d1").innerHTML="<table><tr><td>Please wait while the call is getting assigned</Td></tr></table>"
  25. var xmlhttp=false;
  26. //alert(params);
  27. /*@cc_on @*/
  28. /*@if (@_jscript_version >= 5)
  29. // JScript gives us Conditional compilation, we can cope with old IE versions.
  30. // and security blocked creation of the objects.
  31.  try {
  32.   xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  33.  } catch (e) {
  34.   try {
  35.    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  36.   } catch (E) {
  37.    xmlhttp = false;
  38.   }
  39.  }
  40. @end @*/
  41. if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
  42.     try {
  43.         xmlhttp = new XMLHttpRequest();
  44.     } catch (e) {
  45.         xmlhttp=false;
  46.     }
  47. }
  48. if (!xmlhttp && window.createRequest) {
  49.     try {
  50.         xmlhttp = window.createRequest();
  51.     } catch (e) {
  52.         xmlhttp=false;
  53.     }
  54. }
  55.       //alert("xmlhttp"+xmlhttp);
  56.  try{xmlhttp.open("POST", url, true);}
  57.  catch(e)
  58.  {
  59.       alert("some error");
  60. }
  61.  
  62. //Send the proper header information along with the request
  63. xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  64. xmlhttp.setRequestHeader("Content-length", params.length);
  65. xmlhttp.setRequestHeader("Connection", "close");
  66.  
  67. xmlhttp.onreadystatechange = function() {//Call a function when the state changes.
  68.  
  69.     if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
  70.  
  71.         result=xmlhttp.responseText;
  72.         if(result!=0)
  73.         {
  74.         window.location="assignsdcalls.php?refresh=1";
  75.         }
  76.     }
  77. }
  78. xmlhttp.send(params);
  79. }

Then i changed the above code to the following:

Expand|Select|Wrap|Line Numbers
  1. function submitform()
  2. {
  3. result=0;
  4. //gettting the form values;
  5. var requestid=document.getElementById("requestid").value;
  6. var sdenggname=document.getElementById("sdenggname").value;
  7. var sla=document.getElementById("sla").value;
  8. var helpdesk_comments=document.getElementById("helpdesk_comments").value;
  9. var calltype=document.getElementById("calltype").value;
  10. var status=document.getElementById("reqstatus").value;
  11. var sdenggcomment=document.getElementById("sdenngcomments").value;
  12. //document.form1.submit();
  13. //validation part
  14. if((status=='Closed'||status=='closed')&& sdenggcomment=="")
  15. {
  16. alert("The call under closed status need to have comment")
  17. return false;      
  18. }
  19. if(sdenggname=="--"||sla=="--"||helpdesk_comments==""||calltype=="--")
  20. {
  21. alert("Please make sure that you have filled in all the fields");
  22. return false;
  23. }
  24. //validateion part end
  25. //setTimeout("",1250);
  26. document.getElementById("assigncall").disabled=true;
  27. var domain=document.domain;
  28.  
  29. var url = "http://"+domain+"/crfportal/assignhelpdeskcalls_submit.php"; //url
  30. //var url1 = "http://172.16.140.15/crfportal/assignhelpdeskcalls_submit.php"; //url to be used when the 172.16.140.15 is there.
  31. var params = "requestid="+requestid+"&sdenggname="+sdenggname+"&sla="+sla+"&helpdesk_comments="+helpdesk_comments+"&calltype="+calltype+"&status="+status+"&sdenggcomment="+sdenggcomment+ "&rand=" + (new Date()); //generating parameters
  32. document.getElementById("d1").innerHTML="<table><tr><td>Please wait while the call is getting assigned</Td></tr></table>"
  33. var xmlhttp=false;
  34. xmlhttp=new ajaxRequest();
  35.  try{xmlhttp.open("POST", url, true);}
  36.  catch(e)
  37.  {
  38. try{xmlhttp.open("POST", url1, true);}
  39. catch (e)
  40.      {
  41.       alert("some error");}
  42.  }
  43.  
  44. //Send the proper header information along with the request
  45. xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  46. //xmlhttp.setRequestHeader("Content-length", params.length);
  47. //xmlhttp.setRequestHeader("Connection", "close");
  48.  
  49. xmlhttp.onreadystatechange = function() {//Call a function when the state changes.
  50. //alert(xmlhttp.readyState);
  51.  
  52.     if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
  53.         result=xmlhttp.responseText;
  54.         if(result!=0)
  55.         {
  56.         //document.getElementById("d1").innerHTML="<table><tr><td>"+result+"</Td><td><a href='assignsdcalls.php'>Assign another calls</a></td></tr></table>";
  57. //document.getElementById("assigncall").disabled=true;
  58. window.location="assignsdcalls.php?refresh=1";
  59.         }
  60.     }
  61. }
  62. xmlhttp.send(params);
  63.  
  64. }
The function ajaxrequest is defined as follows:-

Expand|Select|Wrap|Line Numbers
  1. function ajaxRequest(){
  2.  var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
  3.  if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
  4.   for (var i=0; i<activexmodes.length; i++){
  5.    try{
  6.     return new ActiveXObject(activexmodes[i])
  7.    }
  8.    catch(e){
  9.     //suppress error
  10.    }
  11.   }
  12.  }
  13.  else if (window.XMLHttpRequest) // if Mozilla, Safari etc
  14.   return new XMLHttpRequest()
  15.  else
  16.   return false
  17. }
when i give alert on xmlhttp.readystate sometimes i get alert as 1234 very quickly and sometimes it just stops after 1.

Also after going through my code, if you see any thing which is not a good programming practice. please let me know.
Aug 27 '09 #11

Post your reply

Sign in to post your reply or Sign up for a free account.