In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
What this article shares with you is about how to solve the problem of caching the results of IE requests for Ajax. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.
By default, IE caches the result of the Ajax request for the request address. In other words, before the cache expires, multiple Ajax requests for the same address are actually sent to the server for the first time. In some cases, this default caching mechanism is not what we want (such as getting real-time data).
First, the problem reappears.
We use an ASP.NET MVC application to reproduce IE's cache of Ajax request results. In an empty ASP.NET MVC application, we define the following default HomeController, which contains an Action method GetCurrentTime that returns the current time.
Public class HomeController Controller {public ActionResult Index () {return View ();} public string GetCurrentTime () {return DateTime.Now.ToLongTimeString ();}}
The View definition for the default Action method Index is as follows. We use the JQuery method to invoke the GetCurrentTime operation as Ajax every 5 seconds and display the returned results.
@ ViewBag.Title $(function () {window.setInterval (function () {$.ajax ({url'@Url.Action ("GetCurrentTime")', success function (result) {$("ul") .append ("" + result + ");});},);})
Running the program in different browsers will get different output. As shown in the following figure, the real-time time can be displayed in the Chrome browser, but the time displayed in the IE is the same.
Second, solve the problem by adding a suffix to the URL address
Since the results returned by IE for Ajax requests are cached based on the request address, if you do not want this caching mechanism to take effect, we can solve this problem by adding a different suffix to the request address on each request. For this example, we add a query string based on the current time for the request address with the following code, and the real-time time will be displayed in IE after running the program again.
(function () {window.setInterval (function () {$.ajax ({url'@Url.Action ("GetCurrentTime")?'+ new Date () .toTimeString (), success function (result) {$("ul") .append ("" + result + ");});},);})
Third, solve the problem through the Ajax setting of jQuery
JQuery actually has an Ajax setting for this, and we just need to call the $.ajaxSetup method to disable the caching mechanism of Ajaz as follows.
$(function () {$.ajaxSetup ({cache false}); window.setInterval (function () {$.ajax ({url'@Url.Action ("GetCurrentTime")', success function (result) {$("ul"). Append ("+ result +");});},);})
In fact, this mechanism of jQuery is also implemented by adding different query string suffixes to the request address, which can be confirmed by requests intercepted by Fiddler.
Fourth, solve the problem through customized response
We can control the browser's caching of results through the response to the request, so we define the following ActionFilter called NoCacheAttribute. In the implemented OnActionExecuted method, we call the SetCacheability method of the current HttpResponse to set the caching option to NoCache. After this NoCacheAttribute feature is applied to the GetCurrentTime method, you can still get real-time time in IE to run our program.
Public class HomeController Controller {public ActionResult Index () {return View ();} [NoCache] public string GetCurrentTime () {return DateTime.Now.ToLongTimeString ();}} public class NoCacheAttribute FilterAttribute, IActionFilter {public void OnActionExecuted (ActionExecutedContext filterContext) {filterContext.HttpContext.Response.Cache.SetCacheability (HttpCacheability.NoCache);} public void OnActionExecuting (ActionExecutingContext filterContext) {}}
The actual NoCacheAttribute feature ultimately controls the Cache-Control header of the message message and sets it to "no-cache", instructing the browser not to cache the results. The response message for the GetCurrentTime request is shown below:
HTTP/. OK Server ASP.NET Development Server/... Date Thu, Jan GMT X-AspNet-Version.. X-AspNetMvc-Version. The above Cache-Control no-cache Pragma no-cache Expires-Content-Type text/html; charset=utf- Content-Length Connection Close PM is how to solve the problem of caching the results of IE requests for Ajax. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.