2

when i request something via ajax if the input is the search the content seems to be cached? do i add a random number at the end of my query?

/search?input=test

to

/search?input=test&random=283928392

i think this would solve my problem. right? how do i write this in javascript

3 Answers 3

9

At the beginning of your script (before any AJAX) put:

$.ajaxSetup({
  cache: false
});

That will solve your problem because it will automatically add the random number for every jQuery request. If you don't use jQuery for your AJAX this won't work.

Sign up to request clarification or add additional context in comments.

2 Comments

That is very sweet, I was not aware of that configuration!
It is really helpful in big projects. Any of the options on docs.jquery.com/Ajax/jQuery.ajax can be set with this, which can really save time.
0

While this [edit: Ramblingwood's Solution] solves your immediate problem, the answer to your question of how to get the number there is to use javascript's Math.random();

Mozilla Reference (JavaScript 1.5)

MSDN Reference (JScript 8.0)

Should be identical, but including both for completeness

1 Comment

Certainly your solution was the most elegant to the problem at hand (and taught me something new about jQuery), but it's important, when using any framework, I think, to remember the real functions at work here, lest the actual bare-metal of it all become lost to the ages as magic and mirrors.
0

Another option

a) if you hate maths

  Math.random().toString().split('.')[1]
  i.e. for random number 0.3338502143216556, you will get 3338502143216556

b) if you wants to do maths

parseInt(Math.random() * 100000)
   i.e.  for random number 0.3338502143216556 you will get 33385

Oky my point here is to send a random integer value not a floating value, hope you don't mind :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.