Use jQuery to call WebService of other projects
Implement login verification
Enter the username and password in html:
Code
Copy codeThe Code is as follows:
<Table style = "width: 400px">
<Tr>
<Td style = "width: 200px" class = "left">
Login ID:
</Td>
<Td style = "width: 200px" class = "left">
<Input id = "txtLoginID" type = "text" style = "width: pixel PX;" value = ""/>
</Td>
</Tr>
<Tr>
<Td style = "width: 200px" class = "left">
Login Password:
</Td>
<Td style = "width: 200px" class = "left">
<Input id = "txtLoginPW" type = "password" style = "width: pixel PX;" value = ""/>
</Td>
</Tr>
<Tr>
<Td style = "width: 200px" class = "center">
<Input id = "btnSignin" value = "Sign in" class = "button" readonly/>
</Td>
<Td style = "width: 200px" class = "center">
<Input id = "btnSignup" value = "Sign up" class = "button" readonly/>
</Td>
</Tr>
</Table>
Jquery reference and logon events
Code
Copy codeThe Code is as follows:
<Script src = "js/jquery-1.4.2.min.js" type = "text/javascript"> </script>
<Script type = "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)
{
Var user = eval (result. d );
Location. href = "Welcome. aspx? UserID = "+ user. UserID
},
Error: function (result, status)
{
If (status = 'timeout ')
{
Alert ("The request timed out, please resubmit ");
}
Else
{
If (result. responseText! = "")
{
Eval ("exception =" + result. responseText );
Alert (exception. Message );
}
}
}
}
);
}
);
});
$ (Document). ready (function ()
{
$ ('# BtnSignup'). click
(Function ()
{
Location. href = "Signup/Signup. aspx ";
})
});
</Script>
ServiceURL is similar to: var serviceURL = "http: // localhost: 1742/SoldierServices. asmx ";
WebService code:
Code
Copy codeThe Code is as follows:
/// <Summary>
/// Summary description for SoldierServices
/// </Summary>
[WebService (Namespace = "http://tempuri.org/")]
[WebServiceBinding (ConformsTo = WsiProfiles. BasicProfile1_1)]
[ToolboxItem (false)]
// To allow this Web Service to be called from script, using ASP. net ajax, uncomment the following line.
[System. Web. Script. Services. ScriptService]
Public class SoldierServices: System. Web. Services. WebService
{
[WebMethod]
Public User UserLogin (string UserLoginID, string UserLoginPW)
{
LoginBusiness lb = new LoginBusiness ();
Return lb. UserLogin (UserLoginID, UserLoginPW );
}
[WebMethod]
Public User GetUserInfo (string UserID)
{
LoginBusiness lb = new LoginBusiness ();
Return lb. GetUserInfo (UserID );
}
}
Note: [System. Web. Script. Services. ScriptService] is annotated by default. Remove the annotation.