CloudPotato
CloudPotato

Reputation: 1275

jQuery wait for Ajax request until it's done doesn't work

I have a formular, when I press a button I have to handle a async Ajax request. The function nextButtonHandler() should be called after all processes from requestForTariff() are finished. I tried touse the following, but it didn't wait until the request processing is over.

$.when(requestForTariff(requestType.Street)).done(function(){
    nextButtonHandler()
});

Here is my Ajax function:

function requestForTariff(requestParam) {
    $.ajax({
        type: 'POST',
        url: //...,
        data: getRequestJSON(requestParam),
        success: function(data, status) {
            if (status == "success") {
                if (requestParam == requestType.Street) {
                    handleResult(data);
                }
                //...
            }
        },
        contentType: "application/json",
        dataType: 'json',
        error: // ...
    });

Upvotes: 0

Views: 46

Answers (1)

CloudPotato
CloudPotato

Reputation: 1275

Thanks to Adreas comment, changing my requestForTariff() function to

function requestForTariff(requestParam) {
    return $.ajax({
        type: 'POST',
        url: //...,
        data: getRequestJSON(requestParam),
        success: function(data, status) {
            if (status == "success") {
                if (requestParam == requestType.Street) {
                    handleResult(data);
                }
                //...
            }
        },
        contentType: "application/json",
        dataType: 'json',
        error: // ...
    });

solved my problem.

Upvotes: 1

Related Questions