W
W
WebDev2016-04-22 10:46:59
Laravel
WebDev, 2016-04-22 10:46:59

Set beforeSend for all ajax requests?

Hello everyone, in different frameworks, csrf tokens are issued for protection, which the client must send to the server, and if it does not do this or the token is invalid, the request is rejected. After some time, the token goes bad and the requests stop coming. I decided to try to get a new token before each request, is it possible to somehow set a function that will receive a new token for all ajax requests (jQuery)?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WebDev, 2016-04-22
@kirill-93

#$.ajaxPrefilter(function(options) {
        if (!options.beforeSend) {
            $.ajax({
                url  :  '/token',
                beforeSend : function() {},
                success : function(token) {
                    console.log('new token - ' + token);
                }
            });
        }
    });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question