ajaxインプリメンテーションユーザー名が登録されているかどうか
5928 ワード
ajax , , , , , servlet
<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
>
<html>
<body>
:
<input type=
"text"
/> ,
<input type=
"button"
value=
" "
/>
<hr/>
<img height=
"0"
/>
<script type=
"text/javascript"
>
// " " ,
document.getElementsByTagName(
"input"
)[
1
].onclick=function(){
var username = document.getElementsByTagName(
"input"
)[
0
].value;
// ajax
var xhr = createXHR();
//readyState ,
xhr.onreadystatechange=function()
{
//ajax
if
(xhr.readyState==
4
)
{
// ok
if
(xhr.status==
200
)
{
//
var msg = xhr.responseText;
if
(msg==
"YES"
)
{
//
var imgElement = document.images[
0
];
imgElement.setAttribute(
"src"
,
"MsgSent.gif"
);
imgElement.setAttribute(
"height"
,
"40"
);
}
else
{
var imgElement = document.images[
0
];
imgElement.setAttribute(
"src"
,
"MsgError.gif"
);
imgElement.setAttribute(
"height"
,
"40"
);
}
}
}
}
var method =
"POST"
;
var url =
"${pageContext.request.contextPath}/register?time="
+
new
Date().getTime();
var sendData =
"username="
+username;
xhr.open(method,url);
xhr.setRequestHeader(
"content-type"
,
"application/x-www-form-urlencoded"
);
xhr.send(sendData);
}
// AJAX ( XMLHttpRequest)
function createXHR(){
var xhr =
null
;
try
{
//IE
xhr =
new
ActiveXObject(
"microsoft.xmlhttp"
)
}
catch
(e){
try
{
// IE
xhr =
new
XMLHttpRequest();
}
catch
(e){
alert(
" , "
);
}
}
return
xhr;
}
</script>
</body>
</html>
servlet, struts action.