JQuery AJAX calls WebService implementation code

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.