vendredi 10 juin 2016

jQuery When Then Done


I am trying to validate a field off of two calls to the database. it goes to the database and validates if its true or false. I need to chain a few AJAX calls to do this. I am using .when, .then, and .done to do this, but it doesn't seem to be working.

var Validate = function () {
        var isValid = true;
        $errorList.find('li').remove();
        $lblAError.text('');

        $.when(ParcelValidate(isValid))
            .then(AccountValidate(isValid))
            .done(function () {
            return isValid
        });
    };

    var ParcelValidate = function (isValid) {
        return $.ajax({
            url: "../WebServices/ParcelMasterWebService.asmx/IsParcelActive",
            method: "POST",
            data: JSON.stringify({ "pin": $parcID.val() }),
            contentType: 'application/json; charset=utf-8',
            datatype: 'json',
            success: function (data) {
                if (!data.d) {
                    isValid = false;
                    $lblPError.text('!').css(({ "color": "red" }));
                    $errorList.append('<li>Parcel must be on record.</li>').css(({ "color": "red" }));
                }
            },
            fail: function () {
                isValid = false;
                $lblPError.text('!').css(({ "color": "red" }));
                $errorList.append('<li>Unexpected error occured!</li>').css(({ "color": "red" }));
            }
        })
    }
    var AccountValidate = function (isValid) {
        return $.ajax({
            url: "../WebServices/FireProtectMasterWebService.asmx/isAccountActive",
            method: "POST",
            data: JSON.stringify({ "accountID": $parcID.val() }),
            contentType: 'application/json; charset=utf-8',
            datatype: 'json',
            success: function (data) {
                if (data.d) {
                    isValid = false;
                    $lblPError.text('!').css(({ "color": "red" }));
                    $errorList.append('<li>Cannot have duplicate Parcels.</li>').css(({ "color": "red" }));
                }
            },
            fail: function () {
                isValid = false;
                $lblPError.text('!').css(({ "color": "red" }));
                $errorList.append('<li>Unexpected error occured!</li>').css(({ "color": "red" }));
            }
        })
    }

Aucun commentaire:

Enregistrer un commentaire