2016-07-18 33 views
0

我目前正在使用codepen.io處理小型隨機報價生成器(Freecodecamp項目)。你可以在這裏找到它:http://codepen.io/Baumo/pen/VjQbBj?editors=1000getJson只在Google Chrome中工作一次

<head> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
</head> 

<script> 
    $(document).ready(function(e){ 
     $('#getQuote').click(function(){ 
     $.ajax({ 
      dataType: "json", 
      url: 'http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1', 
      success: function(a) { 
      $(".quote").html(a[0].content + "<p>&mdash; " + a[0].title + "</p>"); 
      } 
     }); 
    }); 
    }); 
</script> 

<body> 
    <div class="box"> 
    <div class="message quote"> 

    </div> 
    <div class = "right"> 
     <button class="btn" id="getQuote"> 
     NEW QUOTE 
     </button> 
    </div> 
    </div> 
</body> 

如果我打開的頁面與微軟的邊緣,它工作得很好。但是在谷歌瀏覽器中,「新報價」按鈕僅在我第一次點擊時纔有效。

任何想法如何解決這個問題?

謝謝!

回答

0

Chrome正積極緩存您的ajax請求。將cache: false添加到請求中:

$.ajax({ 
     dataType: "json", 
     url: 'http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1', 
     cache: false, 
     success: function(a) { 
     $(".quote").html(a[0].content + "<p>&mdash; " + a[0].title + "</p>"); 
     } 
    });