Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does ajax solve the caching problem

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces ajax how to solve the cache problem, the article is very detailed, has a certain reference value, interested friends must read it!

This is to reduce the unnecessary burden caused by frequent access to the server, but it also brings some problems that can not be satisfied by some special business logic.

For example:

You need to use a select drop-down list in the foreground as the trigger entry for ajax, present the information returned by server on the page, and update the logic of something actual to the session or database.

Everything is fine when switching options for the first time, that is, when the request is submitted, but if you switch the same options because of the browser's cache, you will not go to server, and the actual dynamic information will be fetched from the cache. As a result, the background logic has not been reached. The code is as follows:

Aspx related code

The copy code is as follows:

Actively set up interest rate incentive health care system

QIWL (H9)

KIWL (H11)

JIWL (H15)

Actively establish interest rate incentive type "self-protection" (market interest rate type)

IIWL

HIWL

Actively establish interest rate incentive type health care ("weight storage" type)

KIWLS

To build an active interest rate and health care system.

ODIWL

JDIWL

HDIWL

Actively establish interest rate incentive type of old insurance ("weight storage" type of rice insurance)

JDISE

Aspx.cs code

The copy code is as follows:

If (! IsPostBack)

{

/ / add client events for doropdownlist

DdlProductList.Attributes.Add ("onchange", "selectChange (this)")

}

Ajax.js code

The copy code is 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 (0jue 1) = = "#") {

Document.getElementById ("systemErrorMsg") [xss_clean] = request.responseText.substring (1)

Document.getElementById ("rate") [xss_clean] = ""

} else {

Document.getElementById ("rate") [xss_clean] = request.responseText

Document.getElementById ("systemErrorMsg") [xss_clean] = ""

}

}

}

Request page code

The copy code is 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 lies in the XmlHttpRequest object, after switching options, it is not every time you go to the logic of the request page. The solution for querying the relevant information is as follows:

Request.setRequestHeader ("If-Modified-Since", "0")

To put it simply, both Last-Modified and If-Modified-Since are HTTP headers used to record the last modification time of the page, but Last-Modified is the HTTP header sent by the server to the client, while If-Modified-Since is the header sent by the client to the server, so you can see that when you request a locally existing cache page again The client sends back the last modified Last-Modified timestamp sent by the server through the If-Modified-Since header, which is for the server to verify whether the client's page is up-to-date. If it is not up-to-date, the new content is returned. If it is up-to-date, it returns 304 to tell the client that its local cache page is up-to-date. So the client can load the page directly from the local, so that the data transmitted on the network will be greatly reduced, and the burden on the server will be reduced at the same time.

There is another solution to release the case, which has not yet been tested and should be feasible in theory, which is to set the header of response on the request page:

Response.AddHeader ("Cache-control", "no-cache")

These are all the contents of the article "how to solve the caching problem in ajax". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report