现在的位置: 首页 > 综合 > 正文

asp.net中手写ajax

2018年01月23日 ⁄ 综合 ⁄ 共 1038字 ⁄ 字号 评论关闭
<
html
>


<
head
>


<
title
>
AjaxTest
</
title
>


<
script
>


var
xmlHttp;
function
createXMLHttpRequest()

{


if
(window.ActiveXObject)
{
xmlHttp
=

new
ActiveXObject(
"
Microsoft.XMLHTTP
"
);

}

else

if
(window.XMLHttpRequest)
{

xmlHttp

=

new
XMLHttpRequest();
}
}
function
startRequest()
{
createXMLHttpRequest();
try

{


var
username
=
document.getElementById(
"
<%=text1
.ClientID
%>"
).value;
xmlHttp.onreadystatechange
=
handleStateChange;

xmlHttp.open(

"
GET
"
,
"
validname.aspx?username=
"
+
username,
true
);
xmlHttp.send(
null
);
}

catch
(exception)
{
alert(
"
xmlHttp Fail
"
);
}
}


function
handleStateChange()
{
if
(xmlHttp.readyState
==

4
)
{


if
(xmlHttp.status
==

200

||
xmlHttp.status
==

0
)
{
var
param
=
xmlHttp.responseText;
document.getElementById(

"
msg
"
).innerHTML
=
param;
}
}

}
</
script
>


</
head
>

<
body
>

<
div
>


<
input
type
="type"
id
="text1"

/><
span
id
="msg"
></
span
><
br
>


<
input
type
="button"
value
="Test"
onclick
="startRequest();"

/>


</
div
>


</
body
>


</
html
>

 

 

public

void
Page_Load()
{
string
userName
=
Request.QueryString
["
username
"
];
if
(iExists(userName))
{
Response.Write(

"
用户名存在
"
);
}

else

{
Response.Write(
"
用户名可用
"
);
}
Response.End();
}
public

bool
iExists(
string
username)
{.....}

抱歉!评论已关闭.