[英]How do I send data using a refresh function in javascript and retrieve another data using the same refresh function
[英]JavaScript: How to “refresh” data from same URL?
擁有此API: http : //quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1
如何在單擊事件后使用純JS請求編寫可下載不同數據的純JS請求?
我從這段代碼中得到的一直都是相同的引用:
function getQuote (cb) {
var xmlhttp = new XMLHttpRequest();
var quoteURL = "http://quotesondesign.com/wp-json/posts?filter[orderby]=rand"
xmlhttp.onreadystatechange = function() {
if (xmlhttp.status == 200 && this.readyState==4) {
cb(this.responseText);
}
};
xmlhttp.open("GET", quoteURL, true);
xmlhttp.send();
}
document.querySelector('button').addEventListener("click", function() {
getQuote(function(quote) {
console.log(quote);
});
})
我嘗試了xmlhttp.abort()和其他東西,但它不想合作。 提前致謝!
這是一個緩存問題。 添加時間戳記作為查詢參數,您應該可以破壞緩存。
您的回復正在被瀏覽器緩存。 避免這種情況的常見技巧是執行請求
http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1&r={random_number}
請注意,r = {random_number}每次都會使URL不同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.