最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery ajax overwrite dataurl beforeSend on specific url - Stack Overflow

programmeradmin3浏览0评论

Question

I wanna set a ajax setting for global ajax handled by jQuery

Condition:

If ajax url is 'www.example', the data (querystring or body) will append token.


I tried two method

.ajaxPrefilter

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

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

});

To add token when url is www.example-> 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\/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.


Test in jsfiddle: /

Thanks for your reading and help :)

Question

I wanna set a ajax setting for global ajax handled by jQuery

Condition:

If ajax url is 'www.example.', the data (querystring or body) will append token.


I tried two method

.ajaxPrefilter

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

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

});

To add token when url is www.example.-> 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\./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.


Test in jsfiddle: http://jsfiddle/qVLN2/2/

Thanks for your reading and help :)

Share Improve this question edited Jul 25, 2013 at 4:16 Cԃաԃ 1,2581 gold badge15 silver badges29 bronze badges asked Jul 25, 2013 at 3:09 Chia-Yu PaiChia-Yu Pai 7211 gold badge5 silver badges19 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

You should notice that the String.replace function doesn't affect the original string!

You can try using settings.url = settings.url.replace(....); in your code.

发布评论

评论列表(0)

  1. 暂无评论