views:

63

answers:

1

I got a jQuery prompt that is a password validation. I use a ajax webservice for this task. now my confusion is how should I handle the ajax call and make function bool?

I started with ajax and webservices about a 2 hours ago so be nice.

        $(document).ready(function() {
    $("#sayHelloButton").click(function() {
            jPrompt('Password:', 'Password', 'Password', function(r) {
                if (CheckPassword(r) == true) window.location = "http://www.asp.net";
                else alert('Wrong password');
            });
        });
    });


function CheckPassword(psw) {
        $.ajax({
            type: "POST",
            url: "dummywebservice.asmx/CheckPassword",
            data: "{'" + $('#name').val() + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json"              
        });
    }

webservice

[WebMethod]
    public bool CheckPassword(string password)
    {
        if(!string.IsNullOrEmpty(password))
        {
            if (password == "testpassword")
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }            
    }
A: 

Change your CheckPassword function to take true and false callbacks like so:

function CheckPassword(psw, ifTrue, ifFalse) {
   $.ajax({
      type: "POST",
      url: "dummywebservice.asmx/CheckPassword",
      data: "{'" + $('#name').val() + "'}",
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function(data, textStatus, XMLHttpRequest) {
          if (data)
             ifTrue();
          else
             ifFalse();
      }
   });
}

And then invoke the function like so:

$(document).ready(function() {
   $("#sayHelloButton").click(function() {
   jPrompt('Password:', 'Password', 'Password', function(r) {
      CheckPassword(r, 
         function ifTrue() {
            window.location = "http://www.asp.net";
         },
         function ifFalse() {
            alert('Wrong password');
         }
      );
   });
Bryan Matthews