JavaScript ajax request

// Ajax Post & Get Methods
var ajax = {
    post: function (method, data, success, error) {
        $.ajax({
            url: GlobalValues.WebApiUrl + method,
            type: "POST",
            cache: false,
            contentType: 'application/json; charset=utf-8',
            transformRequest: function (obj) {
                var str = [];
                for (var p in obj)
                    str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                return str.join("&");
            },
            headers: {
                'X-Auth-Token': localStorage.GlobalUserAPIKey,
                'LoginId': localStorage.GlobalUserId
            },
            data: data,
            success: function (res) {
                if (typeof success == "function") {
                    success(res);
                }
            },
            error: function (err) {
                if (err.status == 401) {
                    redirectToLogin();
                }
                else if (typeof error == "function") {
                    error(err);
                }
                else {
                    Notification.error(GlobalMessages.AjaxError);
                }
                console.log(err);
            }
        });
    },
    get: function (method, data, success, error) {
        var currentRequest = $.ajax({
            url: GlobalValues.WebApiUrl + method,
            type: "GET",
            cache: false,
            contentType: 'application/json; charset=utf-8',
            transformRequest: function (obj) {
                var str = [];
                for (var p in obj)
                    str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                return str.join("&");
            },
            headers: {
                'X-Auth-Token': localStorage.GlobalUserAPIKey,
                'LoginId': localStorage.GlobalUserId
            },
            data: data,
            beforeSend: function (request, settings) {
                //this.requestStartTime = new Date().getTime();
            },
            complete: function (data) {
                //this.requestEndTime = new Date().getTime();
                //this.spentTime = (this.requestEndTime - this.requestStartTime);
                //console.log(this.url +' - '+  Helper.msToTime(this.spentTime));
            },
            success: function (res) {
                if (typeof success == "function") {
                    success(res);
                }
            },
            error: function (err) {
                if (err.status == 401) {
                    redirectToLogin();
                }
                else if (typeof error == "function") {
                    error(err);
                }
                else {
                    Notification.error(GlobalMessages.AjaxError);
                }
                console.log(err);
            }
        });

        return currentRequest;
    },
    delete: function (method, data, success, error) {
        $.ajax({
            url: GlobalValues.WebApiUrl + method,
            type: "DELETE",
            cache: false,
            contentType: 'application/json; charset=utf-8',
            transformRequest: function (obj) {
                var str = [];
                for (var p in obj)
                    str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                return str.join("&");
            },
            headers: {
                'X-Auth-Token': localStorage.GlobalUserAPIKey,
                'LoginId': localStorage.GlobalUserId
            },
            data: data,
            success: function (res) {
                if (typeof success == "function") {
                    success(res);
                }
            },
            error: function (err) {
                if (err.status == 401) {
                    redirectToLogin();
                }
                else if (typeof error == "function") {
                    error(err);
                }
                else {
                    Notification.error(GlobalMessages.AjaxError);
                }
                console.log(err);
            }
        });
    },
    put: function (method, data, success, error) {
        $.ajax({
            url: GlobalValues.WebApiUrl + method,
            type: "PUT",
            cache: false,
            contentType: 'application/json; charset=utf-8',
            transformRequest: function (obj) {
                var str = [];
                for (var p in obj)
                    str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                return str.join("&");
            },
            headers: {
                'X-Auth-Token': localStorage.GlobalUserAPIKey,
                'LoginId': localStorage.GlobalUserId
            },
            data: data,
            success: function (res) {
                if (typeof success == "function") {
                    success(res);
                }
            },
            error: function (err) {
                if (err.status == 401) {
                    redirectToLogin();
                }
                else if (typeof error == "function") {
                    error(err);
                }
                else {
                    Notification.error(GlobalMessages.AjaxError);
                }
                console.log(err);
            }
        });
    },
};


function redirectToLogin() {
    clearLocalStorage();
    window.location.replace(GlobalValues.PageUrl);
}

var data = null;
var LoginService = {
validateLogin: function (params, success, error) {
data = params;
ajax.post("validate-login", data, success, error);
},
    interactLogin: function (params, success, error) {
        data = params;
        ajax.post("interact-login", data, success, error);
    }
};

Comments

Popular posts from this blog

Javascript Error : No 'Access-Control-Allow-Origin' header is present on the requested resource

Merge tools in "Resolve conflicts" not shown