Question

In my application inorder to avoid duplicate Group names Im trying to validate user entered value through AJAX call. But im not receiving expected responseText but im receiving responseText from cache. JS file: try{ if (window.ActiveXObject) //IE var xhr = new ActiveXObject("Microsoft.XMLHTTP"); else if (window.XMLHttpRequest) //other var xhr = new XMLHttpRequest(); else alert("your browser does not support AJAX"); }catch(e){alert("error in creting xmlhttpresponse: "+e);}

    escName= document.forms(0).txtEscalationName.value;
    escId = document.forms(0).hidGroupId.value;

    urlParam = "escName=" +escName +"&escCode=" +escId;
    alert(".."+urlParam);       
    xhr.open("GET", "/myapp/jsp/main/escalationNameCheck.jsp?"+urlParam+"&ran="+Math.random() ,true);

xhr.setRequestHeader("Cache-Control","no-cache");
 xhr.setRequestHeader("Pragma","no-cache"); 
xhr.onreadystatechange = function() {

    if (xhr.readyState == 4)
    {               
        if (xhr.status == 200)
        {   
            if (xhr.responseText != null) 
            {   

            alert(document.getElementById("escalationNameValidate"));

                try{
                var populate = document.getElementById("escalationNameValidate");

                populate.innerHTML = xhr.responseText;
                }catch(e){

                alert("ERROR!...."+e);
            }
            }
            else
            {
                alert("Failed to receive JSP file from the server - file not found.");
                return false;
            }

        }
        else
            alert("Error code " + xhr.status + " received: " + xhr.statusText);
    } else
    alert("not ready");
} 
xhr.send(null); 

After referring here: responseText - XMLHttpRequest I have tried mentioning event like below but it gave exception saying target is null or not an object. xhr.onreadystatechange = function(event) { var xhr = event.target; if (xhr.readyState == 4) { alert("first if");
if (xhr.status == 200) {alert("second if");
if (xhr.responseText != null) {alert("third if"+xhr.responseText);

            alert(document.getElementById("escalationNameValidate"));

                try{
                var populate = document.getElementById("escalationNameValidate");
                alert("11");
                alert("pop.."+populate.innerHTML);
                populate.innerHTML = xhr.responseText;
                }catch(e){

                alert("ERROR!.xhr..."+e);
            }
            }
            else
            {
                alert("Failed to receive JSP file from the server - file not found.");
                return false;
            }
            alert("escNameDuplicate"+document.getElementById("escNameDuplicate"));
        }
        else
            alert("Error code " + xhr.status + " received: " + xhr.statusText);
    } else
    alert("not ready");
} 

So I tried 1. giving cache-control as no-cache in requestHeader 2. Making xhr as local variable by using var xhr while defining it. 3. pointed the event-instance in onreadystatechange function also. Browser version: IE7

Kindly assist. Regards, Nidhya

Was it helpful?

Solution

I got this done, by modifying few options in my IE settings. Even though i close the browser and open IE again, my response was getting cached in the browser. So, what I did was, go to Tools --> Internet Options Select Setting under Browser History Select "Every time i visit the webpage" under "Check for newer versions of stored pages:" save the settings.

Hope it will help someone else like myself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top