Ajax doubt

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nithu25
    New Member
    • Nov 2007
    • 15

    Ajax doubt

    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 "addtoColle ct" 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.j sp". 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.j sp".


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

    Here is the code:



    [code=javascript]

    var xmlHttp;
    var divid;


    function AddtoCollection (id, userid,stit)
    {
    var user = userid;
    var imageid = id;
    xmlHttp=GetXmlH ttpObject();
    if (xmlHttp==null)
    {
    alert ("Your browser does not support AJAX!");
    return;
    }

    var url="addtocol.j sp";
    url=url+"?imgid ="+imageid+"&ui d="+user;
    divid = stit;
    xmlHttp.onready statechange=sta teChanged;
    xmlHttp.open("G ET",url,true) ;
    xmlHttp.send(nu ll);
    xmlHttp.close;
    }
    //}

    function stateChanged()
    {
    if (xmlHttp.readyS tate==4)
    {
    var bdivid = "bc" + divid;
    var adivid = "ac" + divid;
    //document.getEle mentById("txtHi nt").innerHTML= xmlHttp.respons eText;
    document.getEle mentById(bdivid ).style.display = "none";
    document.getEle mentById(adivid ).style.display = "block";
    alert ("'"+divid+" ' Added To Collection");
    }
    }

    function GetXmlHttpObjec t()
    {
    var xmlHttp=null;
    try
    {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest( );
    }
    catch (e)
    {
    // Internet Explorer
    try
    {
    xmlHttp=new ActiveXObject(" Msxml2.XMLHTTP" );
    }
    catch (e)
    {
    xmlHttp=new ActiveXObject(" Microsoft.XMLHT TP");
    }
    }
    return xmlHttp;
    }

    [/code]


    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.Th is works fine in Firefox , but not in IE.
    Last edited by gits; Jan 20 '08, 11:23 PM. Reason: fix code tags
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    IE is probably caching the requests. Add the time to each request to make it unique, e.g. url=url+"?imgid ="+imageid+"&ui d="+user+"&rnd= "+new Date().getTime( );

    Comment

    Working...