Gọi hàm từ server side bằng javascript trong asp.net Website

Gọi hàm từ server side bằng javascript trong asp.net Website

Gọi một hàm từ server side bằng javascript là một câu hỏi được rất nhiều bạn hỏi WEBXAULA với nhiều lý do muốn truy xuất dữ liệu, gọi session hay gọi một hàm đơn giản từ server side bằng javascript nhằm tránh load lại trang. 

Gọi hàm từ server side bằng javascript trong asp.net Website

WEBXAULA sẽ giới thiệu với các bạn cách sử dụng jquery để gọi hàm từ server side.  Ở đây tôi có một form gồm tên người dùng, email, mật khẩu và confirm mật khẩu, tôi sẽ lấy thông tin người dùng nhập và hiện lên form.

Các bạn chuẩn bị giao diện như sau: 

 

<div>
        <table border="0">
            <tr>
                <td>Username</td>
                <td><asp:TextBox ID="txtUsername" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td> Email</td>
                <td><asp:TextBox ID="txtEmail" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td>Password</td>
                <td><asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox></td>
            </tr>
            <tr>
                <td>Confirm</td>
                <td><asp:TextBox ID="txtConfirm" runat="server" TextMode="Password"></asp:TextBox></td>
            </tr>
            <tr>
                <td></td>
                <td><input type="button" id="btnRegister" value="Submit" /></td>
            </tr>
        </table>
    </div>
    <div id="result" style="color:Red"></div>
 
Sau đó bạn add một webservice đặt tên là test và viết một hàm như sau:
 
[WebMethod]
    public string TestThu(string username, string email, string password, string confirm)
    {
       
            return "<b>Dữ liệu trả về</b> <br> Username : " + username + "<br>Email : " + email + "<br/>Mật khẩu: " + password;
           
    }

Bạn lưu ý dòng sau không được comment thì webservice mới có thể gọi được từ javascript:

// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
 [System.Web.Script.Services.ScriptService]

Quay lại code html, bạn gọi Jquery:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>

Sau đó bạn viết hàm javascript như sau:

<script type="text/javascript">
       $(document).ready(function () {
           // Add the page method call as an onclick handler for the div.
           $("#btnRegister").click(function () {
               var username = $("#txtUsername").val();
               var email = $("#txtEmail").val();
               var password = $("#txtPassword").val();
               var confirm = $("#txtConfirm").val();
 
               $.ajax({
                   type: "POST",
                   url: "test.asmx/TestThu",
                   data: "{username: " + username + ",email: " + email + ",password: " + password + ",confirm: " + confirm + "}",
                   contentType: "application/json; charset=utf-8",
                   dataType: "json",
                   success: function (msg) {
                       // Replace the divs content with the page methods return.
                       $("#result").html(msg.d);
                   }
               });
           });
       });
    </script> 

 

Bạn thấy bài viết này như thế nào?: 
Average: 10 (1 vote)
Ảnh của Tommy Tran

Tommy owner Express Magazine

Drupal Developer having 9+ year experience, implementation and having strong knowledge of technical specifications, workflow development. Ability to perform effectively and efficiently in team and individually. Always enthusiastic and interseted to study new technologies

  • Skype ID: tthanhthuy

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
Trạng thái của Drupal 8: Sau tính năng Freeze

Trạng thái của Drupal 8: Sau tính năng Freeze

Over the last year, we've been covering the Drupal community's progress towards releasing Drupal 8.

Thiết kế UI UX, hướng phát triển tiếp theo mà Graphic Designer hướng đến

Thiết kế UI UX, hướng phát triển tiếp theo mà Graphic Designer hướng đến

4 lý do khiến thiết kế UI UX trở thành ngành hot được quan tâm hiện nay:

Để Google Index nhanh hơn

Để Google Index nhanh hơn

Hầu hết những chuyên gia SEO đều khuyên bạn nên mua 1 site domain đã cũ để được Google đánh giá cao hơn và dễ Index hơn, nhưng nếu như bạn bắt đầu tạo 1 website mới thì sao?

Công ty diệt chuột T&C

 

Diet con trung