DEV Community

Spsoi
Spsoi

Posted on

1 1

Ajax Fetch

var $ajaxResponse = null; // глобальная переменная
const ConstructorAjax = function(url, method, async = true, data = []) {
    $ajaxResponse = null;
    let body = {};
    body.url = url;
    body.async = async;
    body.type = method;
    body.dataType = "json";
    if (method == 'POST' || method == 'PUT' || method == 'PATCH' || method == 'DELETE') {
        body.data = data;
    }
    body.success = function(response) {
        console.log(response);
        $ajaxResponse = response;
        return response;
    }
    body.error = function(xhr, ajaxOptions, thrownError) {
        console.log(xhr);
        console.log(ajaxOptions);
        console.log(thrownError);
    }
    console.log(body);
    return $.ajax(body);
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay