Arbejdsglæde
Arbejdsglæde

Reputation: 14098

JQuery ajax return value to other function

Hello I have next function, what always return null as result, but service calling to success branch with any error, I would like to fix it: 1. Remove async attribute, 2. Other function that make GetStore function call should process result of this function. How I can do this in correct way ? ()

Thanks.

 function GetStore() {
        $.ajax({
            url: serviceurl + 'GetSometing',
            type: 'POST',
            contentType: 'application/json',
            dataType: "json",
            async: false,
            success: function (result) {
                return result;
            },
            error: function (xhr, description, error) {
                return null;
            }
        });
    }

Upvotes: 0

Views: 109

Answers (1)

Terry
Terry

Reputation: 14219

If you want to make it asynchronous you can't wait for it to return a value, you have to implement callback functions.

function GetStore(success, error) {
    $.ajax({
        url: serviceurl + 'GetSometing',
        type: 'POST',
        contentType: 'application/json',
        dataType: "json",
        success: success,
        error: error
    });
}

GetStore(function(result) {
    // successful result
}, function(xhr, description, error) {
    // an error occurred
});

Upvotes: 2

Related Questions