ajax with servlet

S

Shinya Koizumi

I am trying to call a servlet i created. Basically this servlet
will let user create a new account at the server.
I have created the function "CreateAccount()" but
http_request.readyState always return me 0

What is wrong with my code?

function CreateAccount()
{
if( window.XMLHttpRequest )
{
http_request = new XMLHttpRequest();
}
else if( window.ActiveXObject )
{
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}
var parameters = "username=" + encodeURI(
document.getElementById("username").value ) +
"&password=" + encodeURI(
document.getElementById("password").value );

http_request.onreadystatechange = RegisterUser();
http_request.open('post', "/servlet/CreateUserAccount", true );
http_request.setRequestHeader("Content-type",
"application/x-www-form-urlencoded");
http_request.setRequestHeader("Content-length", parameters.length);
http_request.setRequestHeader("Connection", "close");
http_request.send(parameters);

}
}

function RegisterUser()
{
alert( "register user: " + http_request.readyState );
if( http_request.readyState == 4 )
{
if( http_request.status == 200 )
{
alert( http_request.responseText );
}
else
{

}
}
}
 
W

web.dev

Shinya said:
http_request.onreadystatechange = RegisterUser();

You are assigning the return value of the function call to
RegisterUser(). You should be assigning a function reference instead:

http_request.onreadystatchange = RegisterUser;
 
S

Shinya

Thanks it solved the previous problem
However, it takes about 10 seconds to get the response.
Do you happen to know why?
 
D

Dag Sunde

How long does it take to get a response if you call it
from an ordinary form, or browse to it manually?
 

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
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top