Pompiuses
05-30-2006, 09:35 AM
I've made a small AJAX script (see below). All it does is make a request to server (when onblur on the input box), gets some xml back from the server and then prints a div-tag on the html page based on the xml.
This all works fine, but it only works one time. Then I have to reload the page for it to work again. Why?? (i'm using IE6 on winxp)
The code is based on this tutorial:
http://www.webpasties.com/xmlHttpRequest/xmlHttpRequest_tutorial_1.html
Perhaps the problem lies within the getHTTPObject() method (which I've just copied from the tutorial)?
</html>
<head>
<script language="javascript" type="text/javascript">
// The url to post to
var url = "http://localhost:8080/secrets/webServices/ajaxValidation.jsp";
// Posts the username to server for validation using AJAX (the http object)
function validateUsername() {
var usernameValue = document.getElementById("username").value;
var ajaxValue = "true";
var registerValue = "true";
var parameters = "username=" + encodeURI(usernameValue) +
"&ajaxValidation=" + encodeURI(ajaxValue) +
"®isterValidation=" + encodeURI(registerValue);
http.onreadystatechange = handleUsernameHttpResponse;
http.open("POST", url, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", parameters.length);
http.setRequestHeader("Connection", "close");
http.send(parameters);
}
// handles the response from the server
function handleUsernameHttpResponse() {
if (http.readyState == 4) { // wait until request is completed
if(http.status == 200) { // check that request is ok
var xmlDocument = http.responseXML; // Use the XML DOM to unpack the xml
var error = xmlDocument.getElementsByTagName('error').item(0).firstChild.data;
document.getElementById('usernameErr').innerHTML = "";
if(error == -1){ // -1 means no error.
document.getElementById('usernameErr').innerHTML = "";
document.getElementById('usernameOK').innerHTML = "Legal username";
}else{
document.getElementById('usernameOK').innerHTML = "";
document.getElementById('usernameErr').innerHTML = "Illegal name";
}
}
}
}
// Creates an HTTP object to enable communication to the server
function getHTTPObject() {
var xmlhttp;
/*@cc_on
@if (@_jscript_version >= 5)
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
@else
xmlhttp = false;
@end @*/
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp = false;
}
}
return xmlhttp;
}
var http = getHTTPObject(); // Create the HTTP object
</script>
</head>
<body>
<div style="color:#FF0000;" id="usernameErr"></div>
<div id="usernameOK"></div>
<br />
<table border="0" width="100%">
<tr>
<td><input type="text" size="20" value="" name="username" id="username" onblur="validateUsername();"></td>
</tr>
</table>
</body>
</html>
This all works fine, but it only works one time. Then I have to reload the page for it to work again. Why?? (i'm using IE6 on winxp)
The code is based on this tutorial:
http://www.webpasties.com/xmlHttpRequest/xmlHttpRequest_tutorial_1.html
Perhaps the problem lies within the getHTTPObject() method (which I've just copied from the tutorial)?
</html>
<head>
<script language="javascript" type="text/javascript">
// The url to post to
var url = "http://localhost:8080/secrets/webServices/ajaxValidation.jsp";
// Posts the username to server for validation using AJAX (the http object)
function validateUsername() {
var usernameValue = document.getElementById("username").value;
var ajaxValue = "true";
var registerValue = "true";
var parameters = "username=" + encodeURI(usernameValue) +
"&ajaxValidation=" + encodeURI(ajaxValue) +
"®isterValidation=" + encodeURI(registerValue);
http.onreadystatechange = handleUsernameHttpResponse;
http.open("POST", url, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", parameters.length);
http.setRequestHeader("Connection", "close");
http.send(parameters);
}
// handles the response from the server
function handleUsernameHttpResponse() {
if (http.readyState == 4) { // wait until request is completed
if(http.status == 200) { // check that request is ok
var xmlDocument = http.responseXML; // Use the XML DOM to unpack the xml
var error = xmlDocument.getElementsByTagName('error').item(0).firstChild.data;
document.getElementById('usernameErr').innerHTML = "";
if(error == -1){ // -1 means no error.
document.getElementById('usernameErr').innerHTML = "";
document.getElementById('usernameOK').innerHTML = "Legal username";
}else{
document.getElementById('usernameOK').innerHTML = "";
document.getElementById('usernameErr').innerHTML = "Illegal name";
}
}
}
}
// Creates an HTTP object to enable communication to the server
function getHTTPObject() {
var xmlhttp;
/*@cc_on
@if (@_jscript_version >= 5)
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
@else
xmlhttp = false;
@end @*/
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp = false;
}
}
return xmlhttp;
}
var http = getHTTPObject(); // Create the HTTP object
</script>
</head>
<body>
<div style="color:#FF0000;" id="usernameErr"></div>
<div id="usernameOK"></div>
<br />
<table border="0" width="100%">
<tr>
<td><input type="text" size="20" value="" name="username" id="username" onblur="validateUsername();"></td>
</tr>
</table>
</body>
</html>