UpTheCreek
UpTheCreek

Reputation: 32391

Jquery: Behaviour of multiple ajax calls to same URL

I came across some suprising behaviour today, and wanted to confirm that this is normal for jquery, and not be doing something daft...

I have a function which makes an ajax call and does something on the promise.done(). Below is a simplified example which serves the same purpose.

function getRandom() {
    console.log('calling');
    var promise = $.ajax({
        url: '/getrandom/',
        type: 'GET'
    });
    promise.done(function (data) {
        console.log('got ' + JSON.stringify(data));
    });
}

For the purposes of this question assume that the server will respond with a random number generated on each request.

If I call this function fast enough so that multiple calls are open before the first response, then I get output like this:

calling
calling
calling
got {num:3452345}
got {num:3452345}
got {num:3452345}
calling
calling
calling
got {num:2342342}
got {num:2342342}
got {num:2342342}

This was quite suprising to me, as it suggests that somehow the promise (or some mechinaism internal to jquery) is being overwritten.

Is this normal expected behaviour?

Thanks

[jQuery v 1.8.2]

Upvotes: 1

Views: 1518

Answers (3)

MANISHDAN LANGA
MANISHDAN LANGA

Reputation: 2237

Add querystring with random char or use timestemp see example

getrandom?t=xyzrandom

Upvotes: 1

maximkou
maximkou

Reputation: 5332

Possible, this result is cached. Try to use ajax "cache: false;" parameter.

Upvotes: 1

TecHunter
TecHunter

Reputation: 6141

Did you try with cache:false?

see Ajax Jquery Api Documentation

Your browser will cache Ajax when doing GET queries. You should check if your server receive 1 query instead of three too.

Upvotes: 4

Related Questions