简体   繁体   中英

Set variable value if jQuery AJAX request is error

I'm working on a form that has 4 different ajax request and I want to check if all 4 are successful. If they all are successful I want to switch step.

Im trying something like this: If i get error on one of the request i want to change the "ss" variable to false.

var ss;

ss = true;

if (ss === true) {
this.owner.switchStep(3);
};

$.ajax({
    url: url,
    type: method,
    data: JSON.stringify(estate),
    beforeSend: function() {},
    success: function(response) {},
    error: function(xhr, thrownError, code) {
        return ss = false;
    },
    complete: function() {}
});


$.ajax({
        ...
        error: function(xhr, thrownError, code) {
            return ss = false;
        },
        ...
    });

$.ajax({
            ...
            error: function(xhr, thrownError, code) {
                return ss = false;
            },
            ...
        });

$.ajax({
            ...
            error: function(xhr, thrownError, code) {
                return ss = false;
            },
            ...
        });

Chain your requests or put them synchronous (setting is async: false ).

Btw are you sure to need 4 ajax requests ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM