• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

AJAX part in jsp is not functioning with IE 6.0

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I have designed a jsp page and have written a javascript code using ajax so that the data submitted to the servlet is sent through this script code.

The jsp page is responsible to display records. For example,
when the page is loaded it displays about 10 records. When i click the next button it does not communicate with the servlet to give me the next 10 records.

The problem is when i tried to test with IE 6 the data gets retreived only once and for the next retreival it does not communicate with the respective servlet at all. With Firefox the same code works fine.

This is the code...

/**
* This function is used to perform ajax call
*
* url - url for request
* formNameOrIndex - either formName or formIndex
* namedTag - name of the tag is to be updated
*/

function retrieveURL(url,formNameOrIndex,namedTag) {

tagName = namedTag;
//get the (form based) params to push up as part of the get request
url=url+getFormAsString(formNameOrIndex);

//Do the Ajax call
if (window.XMLHttpRequest) { // Non-IE browsers
req = new XMLHttpRequest();
req.onreadystatechange = processStateChange;
try {
req.open("GET", url, true);
} catch (e) {
alert("Problem Communicating with Server\n"+e);
}
req.send(null);//if method=get
} else if (window.ActiveXObject) { // IE
req = new ActiveXObject("Microsoft.XMLHTTP");
req.onreadystatechange = processStateChange;
try {
req.open("GET", url, true);
} catch(e) {
alert("Problem Communicating with Server\n"+e);
}
req.send(null);
}
}


/**
* This function used to get reponse from given url when request
status is successful.
*/
function processStateChange() {

if (req.readyState == 4) { // Complete
if (req.status == 200) { // OK response
splitAndReplace(req.responseText);
} else {
alert("Problem with server response:\n " + req.statusText);
}//end if-else
}//end if
}

/**
* This function used to prepare form-bean
*/
function getFormAsString(formNameOrIndex){
//alert('getFormAsString');

//Setup the return String
returnString ="?";
//Get the form values
formElements=document.forms[formNameOrIndex].elements;
//loop through the array , building up the url
//in the form /strutsaction.do&name=value
for(var i=0;i<formElements.length;i++){
//we escape (encode) each value
if(i==0)
returnString=returnString+escape(formElements[i].name)+"="+escape(formElements[i].value);
else
returnString=returnString+"&"+escape(formElements[i].name)+"="+escape(formElements[i].value);
}
alert(returnString);
return returnString;
}

/**
* This function used to split response text into given tag and replace existing with new HTML tags.
*/
function splitAndReplace(responseText) {

//alert('splitAndReplace');

//split document into tag
splittedElements = responseText.split("</"+tagName+">");

//Process each element
for(var i=0;i < splittedElements.length;i++) {

//check that this begins with <tagName id=
//leave other-tag that having no attribute named id
if((tagPos = splittedElements[i].indexOf("<"+tagName +" id=")) >-1 ){

//remove everything before <tagName id=
if(tagPos > 0) {
subString = splittedElements[i].substring(tagPos);
splittedElements[i] = subString;
}//end if

//get the id of tag
//get the name - between the 1st and 2nd quote mark
startNamePos=splittedElements[i].indexOf('"')+1;
endNamePos=splittedElements[i].indexOf('"',startNamePos);
name=splittedElements[i].substring(startNamePos,endNamePos);

//get the content - everything after the first > mark
startContentPos=splittedElements[i].indexOf('>')+1;
content=splittedElements[i].substring(startContentPos);


//alert("name : " + name);
//alert("content : " + content);

//Now update the existing Document with this element
//check that this element exists in the document
if(document.getElementById(name)){
document.getElementById(name).innerHTML = content;
} else {
alert("Element:"+name+"not found in existing document");
}//end if-else
}//end if

}//end for
}

Thanks,
Paul
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to the HTML/Javascript forum where the AJAX gurus hang out.
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not a guru...lol

Well first off open() should be first, than on readystatechange

Is the URl changing for each request? If not it is being cached by the browser. To get around that issue, you would need to add no-cache headers on your serverside page and you can add a timestamp/random number to the url as a querystring parameter. Or you can go with the post instead of a get.

Personally you are wasting lines of code by having an open/send statement for the native object and an open/send statement for the ActiveX. I would say look into prototype.js to do you Ajax calls.

Eric
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Eric Pascarello:
I would say look into prototype.js to do you Ajax calls.



Second that motion.

I've been using Prototype for some time now, not only for its Ajax simplifications, but its other extensions as well, and it's made my life a lot easier.

Anyone who follows my posts knows I'm very skeptical about 3rd party libraries and frameworks, but Prototype is a great example of a well-thought-out and useful library.
reply
    Bookmark Topic Watch Topic
  • New Topic