By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,930 Members | 1,323 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,930 IT Pros & Developers. It's quick & easy.

ajax with servlet

P: n/a
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
{

}
}
}

Dec 10 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

Shinya Koizumi wrote:
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;

Dec 10 '05 #2

P: n/a
Thanks it solved the previous problem
However, it takes about 10 seconds to get the response.
Do you happen to know why?
"web.dev" <we********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...

Shinya Koizumi wrote:
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;

Dec 12 '05 #3

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

--
Dag.

"Shinya" <xx*@smartestdesign.com> wrote in message
news:lcknf.159663$yS6.113623@clgrps12...
Thanks it solved the previous problem
However, it takes about 10 seconds to get the response.
Do you happen to know why?
"web.dev" <we********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...

Shinya Koizumi wrote:
> 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;


Dec 12 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.