Thứ Ba, 26 tháng 3, 2013

Các kỹ thuật sử dụng Ajax với webservice - ASP

Đầu tiên ta tạo 1 file3313dd5a702c8223f0f674d8bc5ede57_54336530.ajaximg1.jpg (955×582)Trong Code behind của WebService.asmx ta phải gỡ ghi chú dòng [System.Web.Script.Services.ScriptService] Nếu không ta không thể gọi từ Script được
c149063ef539730f1fb2a57005bc897b_54336534.ajaximg2.png (541×194)


Tiếp theo ta xây dựng các hàm có kiểu trả về như bình thường (có thể string, int, bool ...)
Vd:
[WebMethod]
public int RestoPassword(string username, string email)
        {
            ibtEntities db = new ibtEntities();
            var user = db.users.FirstOrDefault(x => x.loginName == username);
            if (user != null)
            {
                if (user.email == email)
                {
                    Lib.Users users = new Lib.Users();
                    if (users.ForgetPass(username,email))
                        return 2; // khôi phục thành công
                    else
                        return 3; // khôi phục thất bại
                }
                else
                {
                    return 0; // email ko khớp với username
                }
            }
            else
            {
                return 1; // username ko tồn tại
            }     
        }       

Chú ý là trên mỗi hàm đều phải có [WebMethod] để hàm có thể thực thi được.
Trường hợp nếu bạn muốn sử dụng Session trong hàm thì thay vì khai báo thêm (EnableSession = true)]
Vd: [WebMethod(EnableSession = true)]
Cú pháp gọi session:
string name = HttpContext.Current.Session["CurrentUser"].ToString();


Cú pháp script để gọi hàm từ Service.asmx
$.ajax({
                type: "POST",
                url: "Service.asmx/RestoPassword",
                data: '{"username":"' + username + '","email":"' + emai + '"}',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    $("#waiting").hide();
                    result = msg.d;
                    if (result == 0) {
                        $("#isValidEmail").addClass("info-show"); // email ko khớp với tài khoản                       
                    }
                    else {
                        if (result == 1) {
                            $("#info-username2").addClass("info-show");
                        }
                        else {
                            if (result == 2) {
                                $(".success").show(); // khôi phục thành công
                            }
                            else {
                                $(".fail").show(); // khôi phục thất bại
                            }
                        }
                    }

                }

            });
Chú ý :
url: "Service.asmx/RestoPassword",
Service.asmx : tên file .asmx
RestoPassword : tên hàm (không cần chú ý tới biến)

data: '{"username":"' + username + '","email":"' + emai + '"}',
username, email : tên biến khai báo bên trong hàm ở file .asmx
username : biến chứ giá trị (ví dụ luong, nguyen ...)

msg.d : Giá trị trả về (kiểu var)




Không có nhận xét nào:

Đăng nhận xét