Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize login without refresh by Ajax

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

这篇文章主要为大家展示了"Ajax如何实现无刷新登录",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"Ajax如何实现无刷新登录"这篇文章吧。

先建一个Login.html页面吧,用于填写登录信息,然后再建一个DealData.aspx页面吧(当然,这里用一般处理程序是最好的了:*.ashx),用来处理数据用的。

在login.html页面中其代码如下:

无刷新登录

$(function () {

//元素绑定全局ajaxStart事件

$("#divMsg").ajaxStart(function () {

$(this).show(); //显示span里面的内容

})

//元素绑定全局ajaxStop事件

$("#divMsg").ajaxStop(function () {

$(this).html("请求处理已经完成!").hide();

})

$("#btnSure").click(function () {//点击按钮事件

var $name = $("#txtName");//获取登录名

var $pwd = $("#txtPwd");//获取密码

if ($name.val() != "" && $pwd.val() != "") {

//调用Login()方法

Login($name.val(),$pwd.val());

} else {

if ($name.val() == "") {//如果登录名不为空

alert("登录名不能为空!");

$name.focus();//获取焦点

return false;

} else {

alert("密码不能为空!");

$pwd.focus();

return false;

}

}

})

})

function Login(name, password) {

$.ajax({

type: "POST", //数据请求的方式(post或get),默认为get

url: "DealData.aspx", //发送请求的地址(默认为当前页)

data: "action=Login&date=" + new Date() + "&name=" + name + "&pwd=" + password,//发送到服务器的数据

//登录成功后返回的数据

success: function (data) {

if (data == "True") {//根据返回值进行判断(注意:Ture写成true应该会出错吧!)

alert("登录成功!");

//_window.location = "1.htm";要跳转的页面

} else {

alert("登录名或密码错误!");

return false;

}

}

});

}

登录名:

密 码:

正在发送请求......

在DealData.aspx中,其后台代码如下:

复制代码 代码如下:

public partial class ManageData : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

string name = System.Web.HttpUtility.UrlDecode(Request["name"]);//获取登录名

string pwd = System.Web.HttpUtility.UrlDecode(Request["pwd"]);//获取密码

Response.Write(Login(name, pwd));

Response.End();

}

private bool Login(string name, string pwd)

{

bool result = false;

if (name == "小菜" && pwd=="123456")

{

return true;

}

return result;

}

}

以上是"Ajax如何实现无刷新登录"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report