little Ajax problem

P

Patrick.O.Ige

I have this Ajax script below that returns data ...

var xmlHttp

function showCustomer(str)

{

xmlHttp=GetXmlHttpObject();

if (xmlHttp==null)

{

alert ("Your browser does not support AJAX!");

return;

}

var url="getcustomer.aspx";

url=url+"?employeeID="+str;

//Adds a random number to prevent the server from using a cached file

url=url+"&sid="+Math.random();

//readyState property holds the status of the server response

//each time each time the readyState changes,

//the onreadystatechange function will be executed.

xmlHttp.onreadystatechange=stateChanged;

//To send off a request to the server we must use the open and send method

xmlHttp.open("GET",url,true);

xmlHttp.send(null);

//window.alert(str);

}

//The stateChanged() function executes every time the state of the XMLHTTP

//object changes.

function stateChanged()

{

if (xmlHttp.readyState==4)

{

//The data sent back from the server can be retrieved with the responseText
property.

document.getElementById("txtHint").innerHTML=xmlHttp.responseText;

}

}





function GetXmlHttpObject()

{

var xmlHttp=null;

try

{

// Firefox, Opera 8.0+, Safari or any

xmlHttp=new XMLHttpRequest();

}

catch (e)

{

// Internet Explorer

try

{

xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");

}

catch (e)

{

xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");

}

}

return xmlHttp;

}



In the HTML i have this:-

NAme1:-<input type="text" id="time" name="time">
<input type="button" id="btn"
onclick="showCustomer(document.getElementById('time'));"
value="button2"><div id="txtHint"></div>

But when i clcik the button i get the value:- [Object] instead of and
employeeid of 1,2 etc..
Am i missing something?

What should happen is u type in an integer lets say 1 or 2 and that goes to
the GetCustomer.aspx page and retrieve results
 
L

Laurent Bugnion [MVP]

Hi,

Patrick.O.Ige said:
I have this Ajax script below that returns data ...

I am not sure that you totally understand what you're doing. First, it
was really not necessary to post the whole script here. Then, the
formatting is really messed up. Finally, executing the script in any
debugger (Venkman, Visual Studio, Windows Script Debugger...) would have
shown you quite fast what the problem is.

The function showCustomer needs a string as input. When you use the
"str" parameter, you do:

url=url+"?employeeID="+str;

However, you call the method with:

showCustomer(document.getElementById('time'));

document.getElementById returns a DOM Node object. When you attempt to
use the object as a string, the "toString()" method (which each object
inherits from the "Object" class) is called. Since it has not been
overriden in the Node class, it simply returns [Object].

Call the method using
showCustomer(document.getElementById('time').value);

The value property is (in this case, and only because "time" is a
textfield) a string containing the content of the textfield.

<snip>

HTH,
Laurent
 
P

Patrick.O.Ige

Thanks Laurent Bugnion i had lots of Ajax scripts i had to work with and
make changes :(
Thats why the code was dirty etc..Just missed that part and i'm not that
good with JS.
Thx a lot for pointing that out.



Laurent Bugnion said:
Hi,

Patrick.O.Ige said:
I have this Ajax script below that returns data ...

I am not sure that you totally understand what you're doing. First, it was
really not necessary to post the whole script here. Then, the formatting
is really messed up. Finally, executing the script in any debugger
(Venkman, Visual Studio, Windows Script Debugger...) would have shown you
quite fast what the problem is.

The function showCustomer needs a string as input. When you use the "str"
parameter, you do:

url=url+"?employeeID="+str;

However, you call the method with:

showCustomer(document.getElementById('time'));

document.getElementById returns a DOM Node object. When you attempt to use
the object as a string, the "toString()" method (which each object
inherits from the "Object" class) is called. Since it has not been
overriden in the Node class, it simply returns [Object].

Call the method using
showCustomer(document.getElementById('time').value);

The value property is (in this case, and only because "time" is a
textfield) a string containing the content of the textfield.

<snip>

HTH,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
 
L

Laurent Bugnion [MVP]

Hi,

Patrick.O.Ige said:
Thanks Laurent Bugnion i had lots of Ajax scripts i had to work with and
make changes :(
Thats why the code was dirty etc..Just missed that part and i'm not that
good with JS.
Thx a lot for pointing that out.

No worries. I realized when rereading my post that it sounds much
harsher that I wanted. Sorry about that. I realize that everyone has to
learn. I just wanted to underline how important it is to understand what
the script does and what it expects, especially since JavaScript is a
very loose-typed language, and most errors only appear at runtime.

Courage for your work and greetings,
Laurent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,037
Messages
2,570,371
Members
47,013
Latest member
JewellChes

Latest Threads

Top