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

Ajax doubt

P: 15
Hi All,

I don't know much about Ajax.I am facing some problem while using Ajax along with JSP.

From one jsp file, one function named "addtoCollect" is called which is written in a js file, which is written using Ajax. From there it is redirected to a jsp file called "addtocol.jsp". First time when the function is called it works fine. After that again on calling the same function for same imageid , from the js, it is not getting redirected to the "addtocol.jsp".


I am not able to find out what is the problem.

Here is the code:



Expand|Select|Wrap|Line Numbers
  1.  
  2. var xmlHttp;
  3. var divid;
  4.  
  5.  
  6. function AddtoCollection(id, userid,stit)
  7. {    
  8. var user = userid;
  9. var imageid = id;
  10. xmlHttp=GetXmlHttpObject();
  11. if (xmlHttp==null)
  12.   {
  13.   alert ("Your browser does not support AJAX!");
  14.   return;
  15.   } 
  16.  
  17. var url="addtocol.jsp";
  18. url=url+"?imgid="+imageid+"&uid="+user;
  19. divid = stit;
  20. xmlHttp.onreadystatechange=stateChanged;
  21. xmlHttp.open("GET",url,true);
  22. xmlHttp.send(null);
  23. xmlHttp.close;
  24. }
  25. //}
  26.  
  27. function stateChanged() 
  28. {
  29. if (xmlHttp.readyState==4)
  30. var bdivid = "bc" + divid;
  31. var adivid = "ac" + divid;
  32. //document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
  33. document.getElementById(bdivid).style.display = "none";
  34. document.getElementById(adivid).style.display = "block";
  35. alert ("'"+divid+"' Added To Collection");
  36. }
  37. }
  38.  
  39. function GetXmlHttpObject()
  40. {
  41. var xmlHttp=null;
  42. try
  43.   {
  44.   // Firefox, Opera 8.0+, Safari
  45.   xmlHttp=new XMLHttpRequest();
  46.   }
  47. catch (e)
  48.   {
  49.   // Internet Explorer
  50.   try
  51.     {
  52.     xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  53.     }
  54.   catch (e)
  55.     {
  56.     xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  57.     }
  58.   }
  59. return xmlHttp;
  60. }
  61.  
  62.  

Here the parameters--imageid is the id of the image, userid ..id of the user and stit is the imagename.

This problem is only if i use same imageid and same userid again.If i am adding one imageid then this function will be called and it redirects to addtocol.jsp and works fine. But after that if i am removing that from collection and again adding the same image id (ie, when this function is called), it is not redirecting to addtocol.jsp.This works fine in Firefox , but not in IE.
Jan 19 '08 #1
Share this Question
Share on Google+
1 Reply


acoder
Expert Mod 15k+
P: 16,027
IE is probably caching the requests. Add the time to each request to make it unique, e.g. url=url+"?imgid="+imageid+"&uid="+user+"&rnd="+new Date().getTime();
Jan 21 '08 #2

Post your reply

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