zl程序教程

您现在的位置是:首页 >  其他

当前栏目

jQueryAJAX调用WebService实现代码

代码 实现 调用 webservice JqueryAJAX
2023-06-13 09:14:17 时间
用jQuery调用其他项目的WebService
实现登录验证功能
html输入用户名密码:
代码
复制代码代码如下:

<tablestyle="width:400px">
<tr>
<tdstyle="width:200px"class="left">
LoginID:
</td>
<tdstyle="width:200px"class="left">
<inputid="txtLoginID"type="text"style="width:190px;"value=""/>
</td>
</tr>
<tr>
<tdstyle="width:200px"class="left">
LoginPassword:
</td>
<tdstyle="width:200px"class="left">
<inputid="txtLoginPW"type="password"style="width:190px;"value=""/>
</td>
</tr>
<tr>
<tdstyle="width:200px"class="center">
<inputid="btnSignin"value="Signin"class="button"readonly/>
</td>
<tdstyle="width:200px"class="center">
<inputid="btnSignup"value="Signup"class="button"readonly/>
</td>
</tr>
</table>

Jquery引用和登录事件
代码
复制代码代码如下:

<scriptsrc="js/jquery-1.4.2.min.js"type="text/javascript"></script>
<scripttype="text/javascript"language="javascript">
$(document).ready(function()
{
    $("#btnSignin").click
    (function()
    {
      $.ajax
      (
      {
        type:"POST", 
        contentType:"application/json",
        url:serviceURL+"/UserLogin",
        data:"{UserLoginID:""+$("#txtLoginID").val()+"",UserLoginPW:""+$("#txtLoginPW").val()+""}",     
        dataType:"json",
        success:function(result)
        {
        varuser=eval(result.d);
          location.href="Welcome.aspx?userID="+user.UserID
       },
       error:function(result,status)
       {
       if(status=="timeout")
       {
       alert("Therequesttimedout,pleaseresubmit");
       }
       else
       {
       if(result.responseText!="")
       {
       eval("exception="+result.responseText);
            alert(exception.Message);
            }
          }
        }
      }
      );
    }
    );
 });
 
 $(document).ready(function()
{
    $("#btnSignup").click
    (function()
    {
      location.href="Signup/Signup.aspx";
    })  
 });
</script>

serviceURL类似:varserviceURL="http://localhost:1742/SoldierServices.asmx";
WebService代码:
代码
复制代码代码如下:
///<summary>
///SummarydescriptionforSoldierServices
///</summary>
[WebService(Namespace="http://tempuri.org/")]
[WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
//ToallowthisWebServicetobecalledfromscript,usingASP.NETAJAX,uncommentthefollowingline.
[System.Web.Script.Services.ScriptService]
publicclassSoldierServices:System.Web.Services.WebService
{
[WebMethod]
publicUserUserLogin(stringUserLoginID,stringUserLoginPW)
{
LoginBusinesslb=newLoginBusiness();
returnlb.UserLogin(UserLoginID,UserLoginPW);
}
[WebMethod]
publicUserGetUserInfo(stringUserID)
{
LoginBusinesslb=newLoginBusiness();
returnlb.GetUserInfo(UserID);
}
}

注意:[System.Web.Script.Services.ScriptService]默认是注释的,要把注释去掉