Internet Explorer Caching asp.netmvc ajax results

2020-02-10 02:25发布

I'm having an issue with a page in internet explorer. I have an ajax call that calls a form, in other browser, when I click the link it passes in the controller and load correctly data. but in IE, when its loaded once, it aways brings me the same old results without passing in the controller.

7条回答
放荡不羁爱自由
2楼-- · 2020-02-10 03:04

Try:

[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]

This attribute, placed in controller class, disables caching. Since I don't need caching in my application, I placed it in my BaseController class:

[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
public abstract class BaseController : Controller
{

Here is nice description about OutputCacheAttribute: Improving Performance with Output Caching

You can place it on action too.

查看更多
走好不送
3楼-- · 2020-02-10 03:04

You could try setting the cache option to false:

$.ajax({
    url: '/controller/action',
    type: 'GET',
    cache: false,
    success: function(result) {

    }
});

This option will force the browser not to cache the request.


UPDATE:

Based on the comment you could add a unique timestamp to the url to avoid caching issues:

var d = new Date();
var myURL = 'http://myserver/controller/action?d=' + 
    d.getDate() + 
    d.getHours() + 
    d.getMinutes() + 
    d.getMilliseconds();
查看更多
我命由我不由天
4楼-- · 2020-02-10 03:15

I also found this very useful on a similar (but not identical) issue.

http://forums.asp.net/t/1681358.aspx/1?Disable+cache+in+Ajax+ActionLink+extension+method+in+asp+net+MVC

Basically make sure that you're using POST as opposed to GET in your requests. Doing so seems to prevent IE from caching.

Eg:

@Ajax.ActionLink("Clear Contacts", MVC.Home.ClearContacts(), new AjaxOptions{HttpMethod = "POST", UpdateTargetId="targetDiv"})

查看更多
再贱就再见
5楼-- · 2020-02-10 03:16

actually in IE browser caching not clear automatically. but in chrome scripts working accepted.so you need to try for clearing data in browser level.

查看更多
【Aperson】
6楼-- · 2020-02-10 03:17

You can use HttpMethod = "POST" on your AjaxOptions

var ajaxOpts = new AjaxOptions { UpdateTargetId = "TargetDiv",  HttpMethod = "POST"};

like this exp;

@Ajax.ActionLink("Text","ActionName", new AjaxOptions { UpdateTargetId = "TargetDiv",  HttpMethod = "POST"})
查看更多
冷血范
7楼-- · 2020-02-10 03:22

If you are using the Ajax Helper, you can set the AllowCache parameter to false like this:

@Ajax.ActionLink("AjaxCall", "AjaxMethod", "DeconflictedFiles",
                 new { }, 
                 new AjaxOptions
                 {
                     AllowCache = false,
                 }) 

And IE won't cache the results of the call.

查看更多
登录 后发表回答