jqFetch.js 1.32 KB
Newer Older
chase's avatar
chase committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
/**
 * Created by Administrator on 2019/3/1 0001.
 */
commonModule.factory('jqFetch', ['$translate','$q', 'apiInterceptor', function ($translate, $q, apiInterceptor) {
    'use strict';
    function get(url,params,resType){
        return query(url,"GET",params,resType);
    }
    function post(url,params,resType){
        return query(url,"POST",params,resType);
    }
    function query(url,method,params,resType){
        $('#busy-indicator-container').show();
        var defer = $q.defer();
        window.$.ajax({
            type: method,
            url: url,
            data: method==="POST"?JSON.stringify(params):params,
            dataType: resType ? resType : "json",
            beforeSend: function(request) {
                request.setRequestHeader("Authorization", apiInterceptor.tokenType + ' ' + apiInterceptor.apiToken());
                request.setRequestHeader("Content-Type", 'application/json;charset=UTF-8');
            },
            success: function(result) {
                $('#busy-indicator-container').hide();
                defer.resolve(result);
            },
            error:function(result){
                $('#busy-indicator-container').hide();
                defer.reject(result);
            }
        });
        return defer.promise;
    }
    return{
        get:get,
        post:post
    }
}]);