2014-02-13 77 views
-4

我有這樣的代碼:爲什麼我在變量的末尾有隨機字符串?

<html> 
    <head> 
    <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script> 
    <script> 
$(document).ready(function(){ 
     var j = jQuery.noConflict(); 
     j(document).ready(function() {    
      setInterval(function(i) { 
      var divs = document.getElementsByClassName('post'); 
      var lastPostID = parseInt(divs[0].getAttribute('data-id')); 
      var myurl = "/post/"+parseInt(lastPostID); 
      console.log(myurl, "myurl"); 
      j.ajax({ 
       url: myurl, 
       cache: false, 
       success: function(html){ 
       j("#temp").html(html) 
       j("#temp").prependTo("#allPosts"); 
       } 
      }) 
      }, 5000) 
     }); 
}); 
    </script> 
    </head> 
    <body> 
     <div id="temp" style="display: none;"></div> 
     <div id="allPosts"> 
      <div class='post' data-id='100'>...</div> 
      <div class='post' data-id='20'>...</div> 
      <div class='post' data-id='1'>...</div>   
     </div> 
    </body> 
</html> 

我看到控制檯每5秒:

/post/100 myurl jQuery_div_update.html:12 
OPTIONS file:///post/100?_=1392319636208 

在第一行(console.log...)是URL確定。

但jQuery使用的URL(url: myurl,...)末尾有「?_ = 1392319636208」。爲什麼有隨機字符串?我怎樣才能刪除它?

+4

這是因爲'緩存:FALSE' –

+2

jQuery的增加它會阻止緩存。 –

+0

這是爲了避免緩存,但發佈請求不應該緩存,所以它沒有區別。 'cache:false' < - – epascarello

回答

2

在你的AJAX請求已定義的選項cache爲:

cache: false

從jQuery文檔:

緩存(默認值:true,false爲的dataType '腳本'和'jsonp')

類型:布爾型

如果設置爲false,則會強制請求的頁面不會被瀏覽器緩存爲 。注意:將緩存設置爲false只能正確使用HEAD和GET請求 。它通過在GET參數中追加 「_ = {timestamp}」來工作。 其他類型的請求不需要此參數,IE8中除POST已發送到已由GET請求的URL 外。

這裏是jQuery的文檔的鏈接:https://api.jquery.com/jQuery.ajax/

+3

如果只有每個人都可以訪問您正在討論的這個神祕的jQuery文檔。想象一下,人們會真正知道他們在做什麼。 –

相關問題