문제

This is my very first attempt on ajax! I'm unable to find a good post on the internet from where I could learn and apply to my project.

Let me show you the code straightaway!

    [WebMethod]
    protected bool CheckUsername(string username)
    {
        var check = (from c in dc.dml_np_Users
                     where c.Username.Equals(username)
                     select c).Count();
        if (check > 0)
        {
            return false; //return false if username exist in database
        }
        else
        {
            return true; //true if it doesn't exist, i.e available
        }
    }

markup with jquery is as follows:

<script type="text/javascript">
                            $('#txtUsername').blur(function () {
                                $(function () {
                                    $.ajax({
                                        type: "POST",
                                        url: "Login.aspx.cs/CheckUsername",
                                        data: JSON.stringify({ username: $('#txtUsername').val() }),
                                        contentType: "application/json; charset=utf-8",
                                        dataType: "json",
                                        success: function (data) {
                                            if (data) {
                                                toastr.success('username available');
                                            }
                                            else {
                                                toastr.error('Username already exist');
                                                $('#txtUsername').focus();
                                            }
                                        },
                                        error: function (data) {

                                        }
                                    });
                                });
                            });
                        </script>
                        <asp:TextBox ID="txtUsername" CssClass="form-control" runat="server" placeholder="Username"
                            AutoCompleteType="DisplayName" ValidationGroup="Signup"></asp:TextBox>

Now the jquery should include something like this: if(var available is true) success toastr should display else error toastr should display! I'm using toastr for the first time! Please correct my code!! Forgive me for asking too many things in just one question!!

The above code always shows the error msg!!

도움이 되었습니까?

해결책 2

just do the verification?

                        <script type="text/javascript">
                     $(document).ready(function(){
                        $('#txtUsername').blur(function () {
                                $.ajax({
                                    type: "POST",
                                    url: "Login.aspx.cs/CheckUsername",
                                    data: JSON.stringify({ username: $('#txtUsername').val() }),
                                    contentType: "application/json; charset=utf-8",
                                    dataType: "json",
                                    success: function (data) {
                                        if (data) {
                                            toastr.success('username available');
                                        }
                                        else {
                                            toastr.error('Username already exist');
                                            $('#txtUsername').focus();
                                        }
                                    },
                                    error: function (data) {

                                    }
                                });
                        });
                 });
                    </script>
                    <asp:TextBox ID="txtUsername" CssClass="form-control" runat="server" placeholder="Username"
                        AutoCompleteType="DisplayName" ValidationGroup="Signup"></asp:TextBox>

다른 팁

In my opinion change this url: "Login.aspx.cs/CheckUsername", to this url: "Login.aspx/CheckUsername",

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top