본문 바로가기

개발/javascript Tip

ajaxSetup / beforeSend Event

.ajaxPrefilter

$.ajaxPrefilter( function( options, originalOptions, jqXHR ) {

    // Add data to ajax option
    if (options.url.match(/www\.example\.com/i) !== null) {
        originalOptions.data.token = 'i_am_token'
    }

});

To add token when url is www.example.com-> it not work!

In console/debugger originalOptions Object is added token property, but request sent not having token parameter

.ajaxSetup / beforeSend Event

$.ajaxSetup({
    beforeSend: function(jqXHR, settings) {

        // Only GET Method
        if (settings.url.match(/www\.example\.com/i) == null){
            settings.url.replace(/((\.\/[a-z][0-9])*\?+[=%&a-z0-9]*)&?token=[a-z0-9]*&?([=%&a-z0-9]*)/gi, "$1$3")
        }

    },
    data: {
        token: 'i_am_token'
    }
});

And a reverse resolution, add token for each ajax request.

Same as last one, settings.url changed by string replace in the console/debugger. But request still sent original url.

'개발 > javascript Tip' 카테고리의 다른 글

항상중심에레이어띄우기  (0) 2011.12.03
이미지 미리 로딩시켜놓기  (0) 2011.12.03
쿠키1년간체크  (0) 2011.12.03
금액을 한글로 표기 해주는 스크립트  (0) 2011.12.03
javascript 예약어  (0) 2011.12.03