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
});
}