티스토리 뷰

// 공통 Ajax call
// auth: Authorization
// type: Method(GET, POST, PUT, DELETE)
// data: 객체 리터럴  형식 data={page: 1, recordsPerPage: 10}
// dataType: 데이터 타입
// async: 비동기 여부 (Deafult true 비동기)
// successFn: 성공시
// failFn: 실패시
// completeFn: 성공이후
function ajaxCall(auth, type, url, data, dataType, async, successFn, failFn, completeFn) {
    
    type = type.toUpperCase();
    
    const options = {
        type: type,
        url: url,
        contentType: 'application/json',
        success: successFn,
        error: failFn,
        cache : false,
        headers: {
            'Authorization': auth
        }
    }
    
    if ('GET' === type && !!data) {
    
        url = url + '?';
        for (let key in data) {
            url += key + '=' + encodeURIComponent(data[key]) + '&';
        }
        options.url = url;
        
    } else if ('POST' === type || 'PUT' === type || 'DELETE' === type) {
        options.data = JSON.stringify(data);
    }
    
    if (!!dataType) {
        options.dataType = dataType;
    }
    if (!!async) {
        options.async = async;
    }
    
    if (!!completeFn) {
        options.complete = completeFn;
    }
    
    $.ajax(options);
}
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
링크
«   2024/05   »
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
글 보관함