Home > Web Front-end > JS Tutorial > Solution to IE caching problem in ajax call

Solution to IE caching problem in ajax call

亚连
Release: 2018-05-24 14:19:00
Original
1490 people have browsed it

This article mainly introduces the solution to the IE cache problem in ajax calls. It analyzes the principles and specific solutions of the IE cache mechanism in ajax calls in more detail. It has certain reference value. Friends in need can refer to it

The example of this article analyzes the solution to the IE cache problem in ajax calls. Share it with everyone for your reference. The details are as follows:

Problems discovered during the ajax request call: The background request is a simple .aspx file, and this page has not considered the impact of caching. Use ajax to debug I found that sometimes the results are returned directly without running the background code at all, so it is probably affected by the browser cache. After searching online, it turned out to be a caching problem: "In IE, if the URL submitted by XMLHttpRequest is the same as the history, the cache is used and is not submitted to the server at all. Therefore, the newly submitted data or new data cannot be obtained. data".

The solutions are roughly as follows:

1. Only improve the server side

(1) The background is a simple .aspx file, directly Just add

<%@ OutPutCache Location="None"%>
Copy after login

.

(2) But if the background is an .ashx file, the cache settings are usually modified directly in the class.

context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
Copy after login

2. Only improve the client

(1) Change the request address by adding random numbers or adding timestamps

a, Add random numbers :

var url=&#39;AjaxOperations.aspx?rdm=&#39;+Math.random()+&#39;&action=&#39;+op;
Copy after login

b. Add timestamp:

var url=&#39;AjaxOperations.aspx?dtStamp=&#39;+new Date().getTime()+&#39;&action=&#39;+op;
Copy after login

This solution only needs to add a time or random number as a parameter, and the server can achieve the goal without any changes.

(2) Foreground asynchronous call to set the attributes of the XMLHttpRequest object

Add

XMLHttpRequest.setRequestHeader("If-Modified-Since","0")
Copy after login

before XMLHttpRequest sends the request. I personally think this is the "right way". Because you can't know which ajax requests need to be cached or not (to solve the performance bottleneck of the website, most of them use cached ajax), so every time you send a request, confirm whether you want to cache it, compared to the first one in 2 ( 1) Obviously one less url parameter is written, and there is no need to change the settings on the server side (improvement method 1). However, for the encapsulated ajax library, you may no longer be able to use the XMLHttpRequest object directly outside. The usual setting syntax It may be to pass ("If-Modified-Since","0") as a parameter: ajaxObj.sendPost(other parameter list,...."If-Modified-Since","0"); (post method)

Or ajaxObj.sendGet(other parameter list,...."If-Modified-Since","0"); (get method)

The above is what I compiled for everyone, I hope It will be helpful to everyone in the future.

Related articles:

Detailed explanation of Ajax and form iframe methods to implement file upload

A brief analysis of the clever use of Ajax beforeSend Improve user experience

Quick solution for ajax transmission parameters containing special characters

The above is the detailed content of Solution to IE caching problem in ajax call. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template