jQuery ajax调用webservice注意事项(2)
WebService
<script> $(function() { $("#userloging").show(); //登录框处理开始 //加载登录状态 $.ajax({ type: "POST", //访问WebService使用Post方式请求 contentType: "application/json;charset=utf-8", //WebService 会返回Json类型 url: "/API/Service/UserValidate.asmx/ValidateUserLogState", //调用WebService data: "{}", //Email参数 dataType: 'json', beforeSend: function(x) { x.setRequestHeader("Content-Type", "application/json; charset=utf-8"); }, error: function(x, e) { }, success: function(response) { //回调函数,result,返回值 $("#userloging").hide(); var json = eval('(' + response.d + ')'); var userid = json.user.id; if (userid > 0) { $("#spanusername").html(json.user.name); $("#spanmessagenum").html(json.user.message); $("#userloginsucced").show(); $("#userloginbox").hide(); } } }); //登录 $("#userlogbutton").click(function() { var username = $("#username").val(); var userpwd = $("#userpassword").val(); if (username != "" && userpwd != "") { $("#userloging").show(); $.ajax({ type: "POST", //访问WebService使用Post方式请求 contentType: "application/json;charset=utf-8", //WebService 会返回Json类型 url: "/API/Service/UserValidate.asmx/UserLogin", //调用WebService data: "{userName:'" + username + "',userPwd:'" + userpwd + "'}", //Email参数 dataType: 'json', beforeSend: function(x) { x.setRequestHeader("Content-Type", "application/json; charset=utf-8"); }, error: function(x, e) { }, success: function(result) { //回调函数,result,返回值 $("#userloging").hide(); var json = eval('(' + result.d + ')'); var userid = json.user.id; if (userid > 0) { $("#spanusername").html(json.user.name); $("#spanmessagenum").html(json.user.message); $("#userloginsucced").show(); $("#userloginbox").hide(); } else { switch (userid) { case -2: alert("用户被锁定!请30分钟后再登录!"); $("#username").focus(); break; case -1: alert("用户名或密码错误!请核对您的用户名和密码!"); $("#userpassword").focus(); break; default: alert("登录失败!请核对您的用户名和密码之后重试!"); $("#userpassword").focus(); break; } } } }); } else if (username == "") { alert("用户名不能为空!"); $("#username").focus(); } else if (userpwd == "") { alert("密码不能为空!"); $("#userpassword").focus(); } }); //退出 $("#logout").click(function() { $("#userloging").show(); $.ajax({ type: "POST", //访问WebService使用Post方式请求 contentType: "application/json;utf-8", //WebService 会返回Json类型 url: "/API/Service/UserValidate.asmx/UserLogout", //调用WebService data: "{}", //Email参数 dataType: 'json', beforeSend: function(x) { x.setRequestHeader("Content-Type", "application/json; charset=utf-8"); }, success: function(result) { //回调函数,result,返回值 $("#userloging").hide(); if (result.d > 0) { $("#userloginsucced").hide(); $("#userloginbox").show(); } } }); }); //登录框处理结束 }); </script>
内容版权声明:除非注明,否则皆为本站原创文章。