废话不多说,直接上代码。你会发现,用了JQuery之后是如何简单。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
//后台实例代码 ashx文件(可替换为从数据库中读取)
public
void
ProcessRequest(HttpContext context)
{
context.Response.ContentType =
"text/plain"
;
//context.Response.Write("Hello World");
string
name = context.Request.Params[
"name"
].ToString().Trim();
if
(
"china"
.Equals(name))
{
context.Response.Write(
"1"
);
//1标志login success
}
else
{
context.Response.Write(
"0"
);
//0标志login fail
}
}
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
//前台实例代码 aspx文件
<
html
xmlns
=
"http://www.w3.org/1999/xhtml"
>
<
head
runat
=
"server"
>
<
title
></
title
>
<
script
src
=
"js/jquery-1.4.2.js"
type
=
"text/javascript"
></
script
>
<
script
type
=
"text/javascript"
>
$(function() {
$("#test").live("click", function() {
//alert(0);
$.ajax({
type: 'POST',
url: 'Handler1.ashx',
data: { "name": $("#name").val() },
success: function(data) {
if (1 == data)
alert('login success');
else
alert('login fail');
}
});
});
});
</
script
>
</
head
>
<
body
>
<
form
id
=
"form1"
runat
=
"server"
>
<
div
>
<
input
type
=
"text"
name
=
"name"
id
=
"name"
/>
<
input
type
=
"button"
name
=
"test"
id
=
"test"
value
=
"validate"
/>
</
div
>
</
form
>
</
body
>
</
html
>
|
分别在前台aspx页面和后台ashx页面中输入如上代码,就实现了一个超级简单的Ajax登录,很简单吧?
本文转自 guwei4037 51CTO博客,原文链接:http://blog.51cto.com/csharper/1345991