Ajax 只向服务器传递需要的数据并且无刷新【登陆】代码通用
2013-05-20 16:49
405 查看
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script src="js/Jquery1.7.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { $('#txtUserName').blur(function () { $.ajax({ type: "post", contentType: "application/json", url: "WebService1.asmx/ValidateUser", data: "{username:'" + $('#txtUserName').val() + "'}", success: function (result) { $('#spinfo').text(result.d); } }) }) }) </script> </head> <body> <div> 用户名<input type="text" id="txtUserName" /><span id="spinfo"></span><br /> 密码<input type="text" id="txtPwd" /> </div> </body> </html>
WebService1.asmx
[WebMethod] public string ValidateUser(string username) { if (username == "onlifes") { return "用户名已被占用,请选择其他"; } else { return "可以使用,请继续"; } }
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script src="js/Jquery1.7.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { $('#Button1').click(function () { var username = $('#txtUserName').val(); var pwd = $('#txtPwd').val(); $.ajax({ type: "post", contentType: "application/json", url: "WebService1.asmx/Login", data: "{username:'" + username + "',pwd:'" + pwd + "'}", success: function (bukeyi) { if (bukeyi.d == 'true') { window.location = 'HTMLPage2.htm'; } else { $('#divinfo').text("用户名或密码错误"); } } }) }) }) </script> </head> <body> 用户名<input id="txtUserName" type="text" /><br /> 密码<input id="txtPwd" type="text" /><br /> <input id="Button1" type="button" value="登录" /><br /> <div id="divinfo"></div> </body> </html>
WebService1.asmx
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; namespace WebAjax { /// <summary> /// WebService1 的摘要说明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 [System.Web.Script.Services.ScriptService] public class WebService1 : System.Web.Services.WebService { [WebMethod] public string HelloWorld() { return "Hello World"; } [WebMethod] public string Login(string username, string pwd) { if (username == "admin" && pwd == "888888") { return "true"; } else { return "false"; } } } }
相关文章推荐
- 用ajax的同步请求解决登陆注册需要根据服务器返回数据判断是否能提交的问题
- 用Flask创建登陆服务器,并且获取服务器数据的程序
- Ajax删除成功刷新页面但是数据刷新不了需要手动刷新---详解
- 用ajax提交数据到后台,并且把计算结果从后台把数据转到前台(该过程不刷新页面)
- AJAX Hacks 之HACK2. 使用Request对象与服务器进行数据传递
- 关于向后台请求数据(get请求,无参数传递),返回html代码(实际需要返回的是json数据)的解决方案
- AspNetAjaxPager,Asp.Net通用无刷新Ajax分页控件,支持多样式多数据绑定
- jquery的$('#theForm').serialize()获取表单数据,ajax传递到服务器中文乱码。
- ajax动态无刷新传递数组类型的数据
- 编写html表格输出的时候有时候会出现同一列连续出现两行或者是三行的数据内容一样,并且我们想将这些行合并起来,这个时候我们就需要以下这些代码
- JavaScript 封装Ajax传递的数据代码
- ajax 传值,Ajax: Asynchoronous Javascript and xml (异步的js和xml). 异步刷新,异步传递.替代表单提交数据,回调函数处理返回的数据
- Json数据异步绑定到界面的Table并且自动刷新原理及代码
- Ajax 获取服务器时间[无刷新] 代码通用
- js中对arry数组的各种操作小结 瀑布流AJAX无刷新加载数据列表--当页面滚动到Id时再继续加载数据 web前端url传递值 js加密解密 HTML中让表单input等文本框为只读不可编辑的方法 js监听用户的键盘敲击事件,兼容各大主流浏览器 HTML特殊字符
- AspNetAjaxPager,Asp.Net通用无刷新Ajax分页控件,支持多样式多数据绑定
- Json数据异步绑定到界面的Table并且自动刷新原理及代码
- Ajax请求服务器数据动态刷新HighCharts表格
- 对于web api 从页面post数据到web api,如果用ajax,则可以成功上传到服务器,需要一个类来存储那些变量
- 无刷新显示数据(摘抄)修改。ajax。献给和我一样的人。防止大家看到一堆堆的代码,拿来不能用,改还不知道从那里入手,给人家发消息还不回的人