2011-06-28 35 views
1

我從服務器獲取XML文件功能(JQuery的)如何更改請求以避免緩存結果?

function load_config_xml(){ 
    $.ajax({ 
     url: 'config/conf.xml', 
     dataType: "xml", 
     success:parse_xml, 
     error: function(){ 
      alert("Error during loading xml file"); 
     } 
    }); 
} 

,但它不返回新鮮結果,lighttpd的高速緩存。如何更改請求以避免緩存結果?

+1

可能重複被緩存的響應](http://stackoverflow.com/questions/168963/stop-jquery-load-response-from-being-cached) – redsquare

回答

1

最簡單的解決方法是明確使用POST請求。瀏覽器將不緩存那些:

function load_config_xml(){ 
    $.ajax({ 
     url: 'config/conf.xml', 
     dataType: "xml", 
     type: 'POST',  // here we go 
     success:parse_xml, 
     error: function(){ 
      alert("Error during loading xml file"); 
     } 
    }); 
} 

的jQuery還提供了選項cache,你可以設置爲false。這就產生了一些成果:

cache: false 

基本上jQuery將只需修改爲每個請求,當然,你可以在自己的藏漢做的查詢字符串:[停止jQuery的.load的

url: 'config/conf.xml?cachebuster=' + (+new Date()) 
+0

也許作爲負載的雙關語! – redsquare