0

I know I can use $.when().then() if I have a $.ajax call in the $.when method. The thing is that I'm trying to use these methods while I call for another function in the $.when method and another function in the $.then method. The function I call in $.when method eventually calls a $.ajax method.

My problem is that it won't respond if I try doing it. I want it to do everything in the $.when method and then continue to the $.then method.

My initial function (called when clicking a button) is:

function HideForm(elem) {
    var valid = ValidateEmailPass(elem);
    var user, email;

    if (valid) {
        user_info_all.style.display = "none";
        ShowMessage(RegErrMsg, "נירשם, אנא המתן..");
        var email = document.getElementById('<%= reg_email.ClientID %>'), pass = document.getElementById('<%= reg_pass.ClientID %>'), age = document.getElementById('<%= reg_age.ClientID %>'),
        username = document.getElementById('<%= reg_user.ClientID %>');
        $.ajax({
            type: "POST",
            url: 'Register.aspx/Register',
            data: '{user: "' + username.value + '", pass: "' + pass.value + '", age: "' + age.value + '", mail: "' + email.value + '"}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (validated) {
                ShowMessage(RegErrMsg, validated.d);
                if (validated.d.indexOf('נרשמת בהצלחה') == -1) {
                    user_info_all.style.display = "block";
                }
            },
            failure: function (e) {
                $(reg_user_v).toggleClass('validated', false);
                $(reg_user_v).toggleClass('invalid', false);
                $(reg_pass_v).toggleClass('validated', false);
                $(reg_pass_v).toggleClass('invalid', false);
                $(reg_email_v).toggleClass('validated', false);
                $(reg_email_v).toggleClass('invalid', false);
                $(reg_age_v).toggleClass('validated', false);
                $(reg_age_v).toggleClass('invalid', false);
                ShowMessage(RegErrMsg, "אירעה שגיאה בעת ההרשמה, נסה שנית.");
                user_info_all.style.display = "block";
            }
        });
        return true;
    }
    return false;
}

ValidateEmailPass function calls for multiple ajax requests, so I tried placing this function in $.when and the rest of the function in $.then like this:

function HideForm(elem) {
    var valid;
    $.when(function() { 
        valid = ValidateEmailPass(elem); 
    }).then(function() {
        // The rest of the function here
    });
}
3
  • Please show us your codes. Commented May 15, 2015 at 8:03
  • Let me post it, one minute. Commented May 15, 2015 at 8:04
  • @bipen I have posted the codes. Commented May 15, 2015 at 8:11

1 Answer 1

2

$.when works on promises. In your case the ValidateEmailPass method should return deferred and hence you must change logic as following

  1. ValidateEmailPass must return $.Deferred instance.
  2. Resolve/Reject the deferred instance inside ValidateEmailPass method once the validations are succeeded or failed. If you expect then part should not be called in case of validation failure, you should reject the deferred.

Quoting from jQuery.when documentation

If a single argument is passed to jQuery.when() and it is not a Deferred or a Promise, it will be treated as a resolved Deferred and any doneCallbacks attached will be executed immediately.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.