Ajax Cache Problem Requestheader_ajax related

Source: Internet
Author: User
Tags browser cache
This is to reduce the frequent access to the server caused unnecessary burden on it, but also brought some special business logic can not meet the problem.
For example:
You need a select Drop-down list at the front desk as an AJAX trigger entry, while the information returned by the server is presented to the page, and a logical operation is updated on the session or database for the actual things.
When the first switch option, that is, to submit the request is normal, but if you switch the same option because the browser cache reason, will not go to the server, the actual dynamic information obtained from the cache to fetch. The logic behind the scenes has not been taken. The code is as follows:
ASPX related code
Copy Code code as follows:

<asp:dropdownlist id= "ddlproductlist" runat= "Server" >
<asp:listitem value= "" selected= "True" ></asp:ListItem>
<asp:listitem value= "NULL" > Active interest rate becomes-insurance </asp:ListItem>
<asp:listitem value= "QIWL" > Qiwl (H9) </asp:ListItem>
<asp:listitem value= "KIWL" > Kiwl (H11) </asp:ListItem>
<asp:listitem value= "JIWL" > Jiwl (H15) </asp:ListItem>
<asp:listitem value= "NULL" > Active interest rate becomes insurance (market Gold link type) </asp:ListItem>
<asp:listitem value= "IIWL" >·IIWL</asp:ListItem>
<asp:listitem value= "HIWL" >·HIWL</asp:ListItem>
<asp:listitem value= "NULL" > Active interest rate becomes-insurance (storage storage-heavy vision) </asp:ListItem>
<asp:listitem value= "Kiwls" >·KIWLS</asp:ListItem>
<asp:listitem value= "NULL" >ドル build interest rate becomes insurance </asp:ListItem>
<asp:listitem value= "ODIWL" >·ODIWL</asp:ListItem>
<asp:listitem value= "JDIWL" >·JDIWL</asp:ListItem>
<asp:listitem value= "HDIWL" >·HDIWL</asp:ListItem>
<asp:listitem value= "NULL" > interest rate becomes active old-age insurance insurance (storage-weight-storage-ドル-built) </asp:ListItem>
<asp:listitem value= "Jdise" >·JDISE</asp:ListItem>
</asp:DropDownList>

Aspx.cs Code
Copy Code code as follows:

if (! IsPostBack)
{
Adding Client events for Doropdownlist
DDLPRODUCTLIST.ATTRIBUTES.ADD ("onchange", "Selectchange (This)");
}

Ajax.js Code
Copy Code code as follows:

var request;
function Selectchange (obj) {
Createhttprequest ();
var url = "ajaxservice.aspx?product=" + obj.value;
Request.open ("Get", url,true)
Request.onreadystatechange = resetrate;
Request.send ();
return false;
}
Function Createhttprequest () {
if (window). ActiveXObject) {
request = new ActiveXObject ("Microsoft.XMLHTTP");
} else if (window. XMLHttpRequest) {
request = new XMLHttpRequest ();
}
}
function Resetrate () {
if (request.readystate = = 4) {
If request.responseText.substring (0 , 1) = = "#") {
document.getElementById ("Systemerrormsg"). InnerHTML = request.responseText.substring (1);
document.getElementById ("rate"). InnerHTML = "";
} else {
document.getElementById ("rate"). InnerHTML = Request.responsetext;
document.getElementById ("Systemerrormsg"). InnerHTML = "";
}
}
}

Request page Code
Copy Code code as follows:

protected void Page_Load (object sender, EventArgs e)
{
String productshortname = request.querystring["Product"];
if (ProductShortName!= null && productshortname!= "null")
{
string result = Utility.getproductrate (PackageName);
session["rate"] = result;
Response.Write (Result);
}
}

After analyzing the problem in XMLHttpRequest this object, switching options, not every time to go to the logic of the request page. Query the relevant data solutions are as follows:
Request.setrequestheader ("If-modified-since", "0");
Simply put, last-modified and if-modified-since are all HTTP headers that are used to record the last modification time of a page, but last-modified are HTTP headers sent by the server to the client, and If-modified-since is sent by the client to the server head, you can see, the request of the local cache page, the client will pass the If-modified-since header to the previous server sent over the last-modified last modified timestamp sent back, This is to allow the server side to authenticate, through this timestamp to determine whether the client's page is the latest, if not the latest, then return the new content, if it is the latest, then return 304 to tell the client its local cache page is the latest, so the client can directly load the page from the local, This will greatly reduce the data transmitted over the network, but also reduce the burden on the server.
There is another solution to the case, but untested, theoretically feasible, is to set the response header on the request page:
Response.AddHeader ("Cache-control", "No-cache");
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.