Javascript directly calls server C # code ASP. NET Ajax instance

Source: Internet
Author: User

In MS Ajax, one way for JS to interact with C # is to call WebService, which can be ASMX or WCF. Either way, the system automatically generates a proxy JS class for the developer. The implementation method is as follows:

1. Create a website and add a WCF Service to it (select Ajax-Enabled WCF Service here), as shown in:

2. IDE will automatically generate an SVC file for us, which is an external interface and the background implementation class corresponding to this SVC. Such files will be placed under App_Code, as shown in:

3. modify the code of this class as follows:
Copy codeThe Code is as follows:
[ServiceContract (Namespace = "TestAjax")]
[AspNetCompatibilityRequirements (RequirementsMode = AspNetCompatibilityRequirementsMode. Allowed)]
Public class Service
{
[OperationContract]
Public bool ValidateUser (string uid, string pwd)
{
If (uid = "sa" & pwd = "sa ")
{
Return true;
}
Return false;
}
}

4. now we can call it on the page. First, add a ScriptManager to the page and introduce the WCF WebService we just compiled (the purpose is to generate the JS proxy class at runtime ), as follows:
Copy codeThe Code is as follows:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "Default. aspx. cs" Inherits = "_ Default" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Asp: ScriptManager ID = "ScriptManager1" runat = "server">
<Services>
<Asp: ServiceReference Path = "~ /Service. svc "/>
</Services>
</Asp: ScriptManager>
</Div>
</Form>
</Body>
</Html>

5. Now you can write JavaScript code to directly call the WebService written in C. The JS Code is as follows:
Copy codeThe Code is as follows:
<Script type = "text/javascript">
Function ValidateUser (uid, pwd ){
TestAjax. Service. ValidateUser (uid, pwd, OnSucceed, OnFailed );
}
Function OnSucceed (result ){
If (result = true ){
Window. alert ("verified ");
}
Else {
Window. alert ("Verification Failed! ");
}
}
Function OnFailed (result ){
Window. alert ("operation failed:" + result. _ message );
}
</Script>

6. note that TestAjax is called. service. in the ValidateUser method, the return value of the function is not directly obtained in the code, because the method is used to call server functions asynchronously, the correct solution is to specify two callback functions, OnSucceed and OnFailed. The first function is the callback when the call is successful, and the second is the callback when the call fails. Both functions require one parameter, the OnSucceed parameter is the return value of the server function, while the OnFailed parameter is the error message when a failure occurs. The function is similar to the Exception type, where the error information in the _ message attribute is, _ stack trace information with errors in stackTrace.
7. Do not bother with this callback method! In fact, this is a conventional asynchronous callback mode, which is written in most cases (no matter what language!
8. The complete code on the page is as follows:
Copy codeThe Code is as follows:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "Default. aspx. cs" Inherits = "_ Default" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
<Script type = "text/javascript">
Function ValidateUser (uid, pwd ){
TestAjax. Service. ValidateUser (uid, pwd, OnSucceed, OnFailed );
}
Function OnSucceed (result ){
If (result = true ){
Window. alert ("verified ");
}
Else {
Window. alert ("Verification Failed! ");
}
}
Function OnFailed (result ){
Window. alert ("operation failed:" + result. _ message );
}
Function Button1_onclick (){
Var uid = $ get ("tbxUid"). value;
Var pwd = $ get ("tbxPwd"). value;
ValidateUser (uid, pwd );
}
</Script>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Asp: ScriptManager ID = "ScriptManager1" runat = "server">
<Services>
<Asp: ServiceReference Path = "~ /Service. svc "/>
</Services>
</Asp: ScriptManager>
</Div>
Username: <input id = "tbxUid" type = "text"/> <br/>
Password: <input id = "tbxPwd" type = "text"/>
<Input id = "Button1" type = "button" value = "verify" onclick = "return Button1_onclick ()"/>
</Form>
</Body>
</Html>

9. The running result is as follows:
Verification is performed when both the user name and password are sa.
Verification fails when the user name and friends have a value other than sa.


10. If you have any questions, please Email me: warensoft@foxmail.com

Related Article

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.