1.新建demo.aspx页面。
2.首先在该页面的后台文件demos.aspx.cs中添加引用。
using System.Web.Services;
3.无参数的方法调用.
大家注意了,这个版本不能低于.net framework 2.0。2.0已下不支持的。
后台代码:
[WebMethod]
public static string SayHello()
{
return "Hello Ajax!";
}
JS代码:
$(function() {
$("#btnOK").click(function() {
$.ajax({
//要用post方式
type: "Post",
//方法所在页面和方法名
url: "Demo.aspx/SayHello",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
//返回的数据用data.d获取内容
alert(data.d);
},
error: function(err) {
alert(err);
}
});
//禁用按钮的提交
return false;
});
});
页面代码:
<form id="form1" runat="server">
<div>
<asp:Button ID="btnOK" runat="server" Text="验证用户" />
</div>
</form>
运行效果如下:
3.有参数方法调用
后台代码:
[WebMethod]
public static string GetStr(string str, string str2)
{
return str + str2;
}
JS代码:
$(function() {
$("#btnOK").click(function() {
$.ajax({
type: "Post",
url: "demo.aspx/GetStr",
//方法传参的写法一定要对,str为形参的名字,str2为第二个形参的名字
data: "{'str':'我是','str2':'XXX'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
//返回的数据用data.d获取内容
alert(data.d);
},
error: function(err) {
alert(err);
}
});
//禁用按钮的提交
return false;
});
});
运行效果如下:
4.返回数组方法
后台代码:
[WebMethod]
public static List<string> GetArray()
{
List<string> li = new List<string>();
for (int i = 0; i < 10; i++)
li.Add(i + "");
return li;
}
JS代码:
$(function() {
$("#btnOK").click(function() {
$.ajax({
type: "Post",
url: "demo.aspx/GetArray",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
//插入前先清空ul
$("#list").html("");
//递归获取数据
$(data.d).each(function() {
//插入结果到li里面
$("#list").append("<li>" + this + "</li>");
});
alert(data.d);
},
error: function(err) {
alert(err);
}
});
//禁用按钮的提交
return false;
});
});
页面代码:
<form id="form1" runat="server">
<div>
<asp:Button ID="btnOK" runat="server" Text="验证用户" />
</div>
<ul id="list">
</ul>
</form>
//验证用户登陆
$(function () {
$("#aLogin").click(function () {
$.ajax({
type: "Post",
url: "Home.aspx/Login",
data: { 'username': $("#Home_Index1_username").val(), 'userPWD': $("#userPWD").val(),
'action': 'Login', 'checked': $("#Remember_my").attr("checked"), 'Code': $("#Code").val() },
//contentType: "application/json; charset=utf-8",
//dataType: "json",
success: function (data) {
alert(data);
window.location.reload(true);
},
error: function (err) {
alert(err);
}
});
});
});
<div class="top" id="divmemberlogin" style="display:<%= strloginArea%>;">
<span >用户名:</span>
<input type="text" class="word" id="username" runat="server" οnkeyup="textChanged(event)" />
<span>密码:</span>
<input type="password" class="word" id="userPWD" οnkeyup="textChanged(event)" />
<input maxlength="4" id="Code" type="text" style=" 100px;float:left;" placeholder="验证码"
class="word" οnkeyup="textChanged(event)" />
<span style="75px;"><img src="/Frame/VerifyCode.ashx" id="Verify_codeImag"
width="70" height="28" alt="点击切换验证码"
title="点击切换验证码" style="cursor: pointer; margin-left:-5px;"
οnclick="ToggleCode(this.id, '/Frame/VerifyCode.ashx');return false;" /></span>
<%--<span>验证码:</span>
<input type="text" class="word" />--%>
<a id="aLogin" class="Login" style="text-decoration:none; cursor:pointer;" OnClick="btn_Login">登录
</a>
<%--<asp:Button ID="aLogin" CssClass="Login" Text="登录" runat="server" BorderStyle="None"
OnClick="aLogin_Click" />--%>
<input id="Remember_my" type="checkbox" class="Remember_my" /><span id="spanrememberme">记住我
</span>
<%--<span id="spanrememberme"> <input class="Remember_my" type="checkbox" checked="checked"
runat="server" id="agree_userterm" name="auto_login">记住我</span>--%><a href="http://ayue05.blog.163.com/blog/FindPWD.aspx">忘记密码?</a>
<div class="right"><a href="http://ayue05.blog.163.com/blog/register.aspx">注册</a><a href="http://ayue05.blog.163.com/blog/#">新用户?从这里开始</a></div>
</div>
#region 登录处理(用户名,密码,是否记住我,验证码)
public string Login(string username, string userPWD, string action, string strchecked, string Code)
{
var result = string.Empty;
if (username != null && userPWD != null && Code != null)
{
if (Code.ToLower() == Context.Session["dt_session_code"].ToString().ToLower())
{
Member_InfoIBLL ibllmember = new Member_InfoBLL();
StringBuilder strmem = new StringBuilder();
strmem.Append(" and memberName='" + username + "' and pwd='" + Md5Helper.MD5(userPWD,
32) + "'");
Member_Info memberinfo = ibllmember.GetEntityWhere(strmem);
//IList memberlist =new Member_InfoBLL().GetListWhere(" and memberName='" + username +
"' and pwd='" + Md5Helper.MD5(userPWD, 32)+"'");
if (memberinfo != null)
{
//设置SESSION
Session["index_memberinfo"] = memberinfo;
#region 记录Cookie
if (strchecked == "checked")
{
//记住用户名
HttpCookie CookieUsersName = new HttpCookie("Login_UsersName", username);
Response.Cookies.Add(CookieUsersName);
//十天免登录
//Std.Page.StdUserControl.setSaveTime = 10;
}
else
{
HttpCookie GetCookieUsersName = Request.Cookies["Login_UsersName"];
if (GetCookieUsersName != null && !string.IsNullOrEmpty
(GetCookieUsersName.Value))
{
GetCookieUsersName.Expires = DateTime.Now.AddDays(-1);
Response.Cookies.Add(GetCookieUsersName);
}
}
StdUserControl.CurrentUser = memberinfo;
#endregion
Member_level_infoIBLL ibllmemberlevel = new Member_level_infoBLL();
StringBuilder strmemlevel = new StringBuilder();
strmemlevel.Append(" and levelId='" + memberinfo.levelId.ToString() + "'");
string strlevelname = ibllmemberlevel.GetEntityWhere
(strmemlevel).levelName.ToString();
StringBuilder strmember = new StringBuilder();
strmember.Append("<div class='task'><div class='img'><img width='75' height='60'
src='http://ayue05.blog.163.com/blog/images/pic02.jpg'></div><div class='right'><p><span class='red'>" + memberinfo.memberName.ToString() +
"</span>(" + (memberinfo.isAduit.ToString() == "1" ? "已激活" : "未激活") + ")</p><p>上次登录时间:
</p><p>" + memberinfo.lastLoginDate.ToString().Substring(0, memberinfo.lastLoginDate.ToString().IndexOf("
")) + "</p></div></div>");
strmember.Append("<p>账户余额:<span class='red'>¥" + memberinfo.Balance.ToString()
+ "</span><a class='blue' href='http://ayue05.blog.163.com/blog/#'>账户设置</a></p>");
strmember.Append("<p>积分:<span class='red'>" + memberinfo.Integration.ToString() +
"</span><a class=' blue' href='http://ayue05.blog.163.com/blog/#'>兑换</a></p>");
strmember.Append("<p>发布点个数:<span class='red'>97.195</span>个</p>");
strmember.Append("<p>收藏点个数:<span class='red'>8</span>个</p>");
strmember.Append("<p>VIP:" + (strlevelname == "VIP会员" ? "是" : "否") + "</p>");
strmember.Append("<p>信托卡:单钻信托卡</p>");
strmember.Append("<p>商保:否</p>");
strmember.Append("<p class='task_bom'><a href='http://ayue05.blog.163.com/blog/#'>发布任务</a></p>");
strmember.Append("");
//result = strmember.ToString();
Session["index_memberinfoArea"] = strmember.ToString();
result = "登陆成功";
}
else
{
Session["index_memberinfoArea"] = "";
result = "登陆失败";
}
}
else
{
result = "验证码错误";
}
}
else
{
result= "请输入完整";
}
Response.Write(result);
Response.End();
return result;
}
#endregion
home.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Text;
using Shualo.Business;
using Shualo.Entity;
using Shualo.IBusiness;
using DotNet.Kernel;
using System.Collections;
using DotNet.Utilities;
using Shualo.Page;
namespace Shualo.WEB.Home
{
public partial class Home : System.Web.UI.Page
{
public string strLoginName = string.Empty;
public string index_memberinfoArea = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
var action = Request["action"];
switch (action)
{
case "Login":
var name = Request["username"];
var pws = Request["userPWD"];
var strchecked = Request["checked"];
var Code = Request["Code"];
Login(name, pws, action, strchecked, Code);
break;
case "spTaskOver":
var memberuname = Request["memberuname"];
spTaskOver(memberuname);
break;
default:
break;
}
}
#region 登录处理(用户名,密码,是否记住我,验证码)
public string Login(string username, string userPWD, string action, string strchecked, string Code)
{
var result = string.Empty;
if (username != null && userPWD != null && Code != null)
{
if (Code.ToLower() == Context.Session["dt_session_code"].ToString().ToLower())
{
Member_InfoIBLL ibllmember = new Member_InfoBLL();
StringBuilder strmem = new StringBuilder();
strmem.Append(" and memberName='" + username + "' and pwd='" + Md5Helper.MD5(userPWD, 32) + "'");
Member_Info memberinfo = ibllmember.GetEntityWhere(strmem);
//IList memberlist =new Member_InfoBLL().GetListWhere(" and memberName='" + username + "' and pwd='" + Md5Helper.MD5(userPWD, 32)+"'");
if (memberinfo != null)
{
//设置SESSION
Session["index_memberinfo"] = memberinfo;
#region 记录Cookie
if (strchecked == "checked")
{
//记住用户名
HttpCookie CookieUsersName = new HttpCookie("Login_UsersName", username);
Response.Cookies.Add(CookieUsersName);
//十天免登录
//Std.Page.StdUserControl.setSaveTime = 10;
}
else
{
HttpCookie GetCookieUsersName = Request.Cookies["Login_UsersName"];
if (GetCookieUsersName != null && !string.IsNullOrEmpty(GetCookieUsersName.Value))
{
GetCookieUsersName.Expires = DateTime.Now.AddDays(-1);
Response.Cookies.Add(GetCookieUsersName);
}
}
StdUserControl.CurrentUser = memberinfo;
#endregion
Member_level_infoIBLL ibllmemberlevel = new Member_level_infoBLL();
StringBuilder strmemlevel = new StringBuilder();
strmemlevel.Append(" and levelId='" + memberinfo.levelId.ToString() + "'");
string strlevelname = ibllmemberlevel.GetEntityWhere(strmemlevel).levelName.ToString();
StringBuilder strmember = new StringBuilder();
strmember.Append("<div class='task'><div class='img'><img width='75' height='60' src='http://ayue05.blog.163.com/blog/images/pic02.jpg'></div><div class='right'><p><span class='red'>" + memberinfo.memberName.ToString() + "</span>(" + (memberinfo.isAduit.ToString() == "1" ? "已激活" : "未激活") + ")</p><p>上次登录时间:</p><p>" + memberinfo.lastLoginDate.ToString().Substring(0, memberinfo.lastLoginDate.ToString().IndexOf(" ")) + "</p></div></div>");
strmember.Append("<p>账户余额:<span class='red'>¥" + memberinfo.Balance.ToString() + "</span><a class='blue' href='http://ayue05.blog.163.com/blog/#'>账户设置</a></p>");
strmember.Append("<p>积分:<span class='red'>" + memberinfo.Integration.ToString() + "</span><a class=' blue' href='http://ayue05.blog.163.com/blog/#'>兑换</a></p>");
strmember.Append("<p>发布点个数:<span class='red'>97.195</span>个</p>");
strmember.Append("<p>收藏点个数:<span class='red'>8</span>个</p>");
strmember.Append("<p>VIP:" + (strlevelname == "VIP会员" ? "是" : "否") + "</p>");
strmember.Append("<p>信托卡:单钻信托卡</p>");
strmember.Append("<p>商保:否</p>");
strmember.Append("<p class='task_bom'><a href='http://ayue05.blog.163.com/blog/#'>发布任务</a></p>");
strmember.Append("");
//result = strmember.ToString();
Session["index_memberinfoArea"] = strmember.ToString();
result = "登陆成功";
}
else
{
Session["index_memberinfoArea"] = "";
result = "登陆失败";
}
}
else
{
result = "验证码错误";
}
}
else
{
result= "请输入完整";
}
Response.Write(result);
Response.End();
return result;
}
#endregion
#region 退出登陆
[WebMethod]
public static string LoginOut()
{
HttpContext.Current.Session.Clear();
return "退出成功";
}
#endregion
#region 我要接手,是否已登陆,是否自己的任务
//[WebMethod]
public string spTaskOver(string memberuname)
{
//如果用户有登陆,且非自己发的任务。则可接手
//if (HttpContext.Current.Session["index_memberinfo"] != null)
//{
//}
//else
//{
//}
//string result = "成功";
//return result;
Response.Write(memberuname);
Response.End();
return memberuname ;
}
#endregion
}
}
$(function () {
//获取手机验证码
$("#GetPhoneCode").click(function () {
$.ajax({
url: '/Home/Member_Register.ashx',
type: 'post',
data: "action=GetPhoneCode&phoneValues=" + $('#phoneValues').val(),
dataType: 'json',
success: function (d) {
if (d.isSucceed == true) {
$("#GetPhoneCode").attr({ "disabled": "disabled" });//禁用获取手机验证码按钮
$("#GetPhoneCode").addClass("BtnDisabledCss");
ForbiddenGetPhoneCodeBtn(60);//禁用获取手机验证码按钮60秒
$('#divID').hLoading({ type: 'success', timeout: 5000, msg: d.info });
} if (d.isSucceed == false) {
$('#divID').hLoading({ type: 'fail', timeout: 5000, msg: d.info });
}
}
});
});
});
$(function () {
//获取手机验证码
$("#GetPhoneCode").click(function () {
$.ajax({
url: '/Home/Member_Register.ashx',
type: 'post',
data: "action=GetPhoneCode&phoneValues=" + $('#phoneValues').val(),
dataType: 'json',
success: function (d) {
if (d.isSucceed == true) {
$("#GetPhoneCode").attr({ "disabled": "disabled" });//禁用获取手机验证码按钮
$("#GetPhoneCode").addClass("BtnDisabledCss");
ForbiddenGetPhoneCodeBtn(60);//禁用获取手机验证码按钮60秒
$('#divID').hLoading({ type: 'success', timeout: 5000, msg: d.info });
} if (d.isSucceed == false) {
$('#divID').hLoading({ type: 'fail', timeout: 5000, msg: d.info });
}
}
});
});
});