Q BgQuestion:

Novice
Karma Points: 36
Respect (60%):
posted by  Hafiz on 5/18/2008 3:41:35 AM  |  status: Closed  

what is the error in this simple AJAX code

Course Textbook Chapter Problem
Software Design N/A N/A N/A
Question Details:
<html>
<head>
<script>
var xobject=connectIt();
var handler=handler();
//----------------------------------------------------------------------------
function connectIt(){
var xobject;
try{
xobject=new XMLHttpRequest();
}
catch (e){
try{
xobject=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){
try{
xobject=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e){
}
}
}
if(!xobject){
alert("Sorry your browser do not support AJAX or may be we are unable to find its XML http object");
}
else{
return xobject;
}
}
//----------------------------------------------------------------------------
function process(){
docum=document.f1;
if(docum.username.value.length < 1 || docum.pass.value.length < 1){
alert("You have not entered username and password correctly");
docum.username.focus();
}
else{
username=docum.username.value;
pass=docum.pass.value;
if(xobject){
try{
xobject.open("GET","login.php",true);
xobject.onreadystatechange=handler;
xobject.send("null");
}
catch(e){
alert("Can't connect to server:\n"+e.toString());
}
}
}
return false;
}
//-------------------------------------------------------------------------
function handler(){
if(xobject.readystate==4){
if(xobject.status==200){
try{
response=xobject.responseText;
document.getElementById("testingDiv").innerHTML=response;
}
catch (e){
alert("Due to some reason error in reading or manipulting response "+e.toString());
}
}
else{
alert("Error in retrieving data "+xobject.statusText);
}
}
}
//-----------------------------------------------------------------------------
</script>
</head>
<body>
<form name="f1" >
<input type="text" name="username" />
<input type="password" name="pass" />
<input type="button" onClick="process()" />
</form>
<div id="testingDiv">

</div>
</body>
</html>


AAnswers:

Answer Question
Scholar
Karma Points: 332
posted by catherine chrysler on 5/20/2008 8:05:43 AM  |  status: Live
Asker's Rating: Helpful   
Hafiz's comment:
"Thanks Mathew JR II , I really apreciae your effort . I think it was really the problem due to the error that you jighlight on line 57 (Camel Back). "
Response Details:
The following code is in working condition -

<html>
<head>
<script>
var xobject=connectIt();
//var handler=handler();
//----------------------------------------------------------------------------
function connectIt(){
var xobject;
try{
xobject=new XMLHttpRequest();
}
catch (e){
try{
xobject=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){
try{
xobject=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e){
}
}
}
if(!xobject){
alert("Sorry your browser do not support AJAX or may be we are unable to find its XML http object");
}
else{
return xobject;
}
}
//----------------------------------------------------------------------------
function process(){
docum=document.f1;
if(docum.username.value.length < 1 || docum.pass.value.length < 1){
alert("You have not entered username and password correctly");
docum.username.focus();
}
else{
username=docum.username.value;
pass=docum.pass.value;
if(xobject){
try{
//xobject.open("GET","login.php",true);
xobject.open("GET","serve.asp",true);
xobject.onreadystatechange=handler;
xobject.send("null");
}
catch(e){
alert("Can't connect to server:\n"+e.toString());
}
}
}
return false;
}
//-------------------------------------------------------------------------
function handler(){
if(xobject.readyState==4){
if(xobject.status==200){
try{
response=xobject.responseText;
document.getElementById("testingDiv").innerHTML=response;
}
catch (e){
alert("Due to some reason error in reading or manipulting response "+e.toString());
}
}
else{
alert("Error in retrieving data "+xobject.statusText);
}
}
}
//-----------------------------------------------------------------------------
</script>
</head>
<body>
<form name="f1" >
<input type="text" name="username" />
<input type="password" name="pass" />
<input type="button" onClick="process()" />
</form>
<div id="testingDiv">

</div>
</body>
</html>


I traced the following errors in your code that was causing the error -
1.  At line 5 you don't need to put handler function definition in a var, so I commented that line.
//var handler=handler();
2.  At line 57 the readyState property is in camel case i.e. you should have written it as readyState instead of readystate.

rest code is fine.




Answer Question
Ask New Question

Join Cramster's Community

Cramster.com brings together students, educators and subject enthusiasts in an online study community. With around-the-clock expert help and a community of over 100,000 knowledgeable members, you can find the help you need, whenever you need it. Join for free today » How Cramster is different than tutoring »