Solution to only return cached content when Ajax page parameters are the same

Source: Internet
Author: User

Solution to only return cached content when Ajax page parameters are the same

Often use Ajax to write some pages without refreshing content to get the page, this approach is very convenient, but one of the problems is that if the two submitted parameters are the same, the returned content only returns the last obtained content, if we have modified the parameters for the first time, the second call will find that the page has not changed. In this case, the native cache is checked first for Ajax acquisition, and if the native cache already has the same content, the remote server is not accessed. Such operations can improve speed and reduce server pressure. But the drawbacks are also obvious.

To solve this problem. We must add an extra parameter to the Fetch page. The simpler approach is to use a random number.

Examples are as follows

function idCheck() { //参数调用函数

var f = document.modify_form;

var book_num = f.book_num.value;

if(book_num=="") {

window.alert("图书编号不能为空");

f.book_num.focus();

return false;

}

//加一个随机数//////////////////////////////

var number = Math.random();

number = number * 1000000000;

number = Math.ceil(number);

//////////////////////////////////////////

send_request('get_book.php?book_num='+book_num+'&ranum='+number); // 后面的 “ranum=number”是额外加的

}

This avoids the problem of returning the same content to the same parameter page.

There is also a way to add code to the page that is called to prevent this page from being cached

HTM Web page

<metahttp-equiv="pragma"content="no-cache">

<metahttp-equiv="cache-control"content="no-cache,must-revalidate">

<metahttp-equiv="expires"content="wed,26feb199708:21:57gmt">

Or<metahttp-equiv="expires"content="0">

ASP Web page

response.expires=-1

response.expiresabsolute=now()-1

response.cachecontrol="no-cache"

PHP Web page

header("expires:mon,26jul199705:00:00gmt");

header("cache-control:no-cache,must-revalidate");

header("pragma:no-cache");

JSP Web page

response.addHeader("pragma", "no-cache");

response.addHeader("cache-control", "no-cache,must-revalidate");

response.addHeader("expires", "0");

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.