본문 바로가기
Programming/jQuery

jQuery global Ajax event handler

by 신규하 2016. 10. 21.

jQuery global Ajax event handler

jquery를 사용하면서 post를 자주 사용하는데..
유저가 로그인이 풀렸는지 확인하기 위해서 이것저것 보다..
ajax 이벤트가 글로벌로 올라 오는게 있길래 끄적여 봅니다.

아래와 같이 사용하면 됩니다.

$(document).ajaxSuccess(
    function(event, xhr, settings){
        var response;
        try {
            response = $.parseJSON(xhr.responseText);
        } catch(e) {
            response = null;
        }
        if (response) {
            console.log(response);
        }else{
            console.log(xhr.responseText);
        }
    }
);

출처 : http://api.jquery.com/category/ajax/global-ajax-event-handlers/


댓글