[英]JavaScript: How to “refresh” data from same URL?
Having this API: http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1 拥有此API: http : //quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1
How can I write using pure JS request that downloads me different data after button click event? 如何在单击事件后使用纯JS请求编写可下载不同数据的纯JS请求?
All I get from this code is the same quote all the time: 我从这段代码中得到的一直都是相同的引用:
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);
});
})
I tried xmlhttp.abort() and stuff but it didnt want to cooperate. 我尝试了xmlhttp.abort()和其他东西,但它不想合作。 Thanks in advance! 提前致谢!
This is a caching problem. 这是一个缓存问题。 Add a timestamp as a query parameter and you should be able to bust the cache. 添加时间戳记作为查询参数,您应该可以破坏缓存。
Your response is being cached by the browser. 您的回复正在被浏览器缓存。 A common trick to avoid this is to perform a request to 避免这种情况的常见技巧是执行请求
http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1&r={random_number}
Notice how the r={random_number} will make the URL different each time. 请注意,r = {random_number}每次都会使URL不同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.