开发者

Internet Explorer Caching asp.netmvc ajax results

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.


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.


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();


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"})


I've blogged about fixing the IE cache issue for both jQuery and the MS client library:

http://yoavniran.wordpress.com/2010/04/27/ie-caching-ajax-results-how-to-fix/

Hope this helps!


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"})


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜