Javascript 按钮JQuery事件只工作一次,为什么? $(“按钮”)。在(“单击”,函数(){ $.getJSON(“http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts\u per\u page]=1”,函数(json){ $(“.author”).html(json[0].title); $(“.quote”).html(“”+json[0].content+”); }); });
情况:单击,数据加载。我再次点击,没有任何变化 Codepen: 参考:尝试以下方法: 也许这对你有帮助Javascript 按钮JQuery事件只工作一次,为什么? $(“按钮”)。在(“单击”,函数(){ $.getJSON(“http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts\u per\u page]=1”,函数(json){ $(“.author”).html(json[0].title); $(“.quote”).html(“”+json[0].content+”); }); });,javascript,jquery,ajax,rest,api,Javascript,Jquery,Ajax,Rest,Api,情况:单击,数据加载。我再次点击,没有任何变化 Codepen: 参考:尝试以下方法: 也许这对你有帮助 $(document.body){ $.getJSON(“http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts\u per\u page]=1”,函数(json){ $(“.author”).html(json[0].title); $(“.quote”).html(“”
$(document.body){
$.getJSON(“http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts\u per\u page]=1”,函数(json){
$(“.author”).html(json[0].title);
$(“.quote”).html(“”+json[0].content+”);
});
});
问题在于第一个响应正在被缓存。您可以通过添加停止缓存的$.ajaxSetup()
调用来解决此问题:
<script>
$("button").on("click", function() {
$.getJSON("http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1", function(json) {
$(".author").html(json[0].title);
$(".quote").html('"'+json[0].content+'"');
});
});
</script>
或者,使用$.ajax()
并直接在设置中设置缓存
:
$.ajaxSetup({
cache: false
})
控制台中有错误吗?显然,在第一次getJSON调用之后不会发生任何事情,因为您已经第一次呈现了html。如果请求未在第二次触发,则需要检查错误。是否确定服务器上的数据在两次单击之间发生更改?按钮是否为
.author
和/或的子元素?如果是这样,你必须使用它来work@empiric不是孩子。@Camjohnson 26您可以自己用url检查。它改变了。没有改变任何东西。这就是我正在搜索的,记不起这是一个带有CORS头的资源的确切代码吗?@Rorymcrossan仍然没有找到一种方法来导航代码笔界面。stacksnippets使用http://null
作为源站,但能够使用plnkr的
元素获取资源。注)代码>以下成功
atjavascript
问题。
$("button").on("click", function() {
$.ajax({
url: 'http://quotesondesign.com/wp-json/posts',
type: 'get',
cache: false,
data: 'filter[orderby]=rand&filter[posts_per_page]=1',
success: function(json) {
$(".author").html(json[0].title);
$(".quote").html('"' + json[0].content + '"');
}
});
});