vohrahul vohrahul - 6 months ago 34
Ajax Question

Boolean response from c# web method doesn't work

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!!

Answer

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>