我正在使用设计应用程序接口 https://quotesondesign.com/api-v4-0/上的报价,以显示设计师在我的网站上的随机报价。
然而,的想法是显示报价,但只由特定的设计师随机。
我下面的代码不能工作。你知道我为什么要这么做,又该怎么做才能得到想要的结果吗?
$.ajaxSetup(
{ cache: false}
);
function newQuote(){
$.getJSON('https://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1&callback=', function(json,data){
// Filter to return quotes by Steve Jobs only
return json.filter(function(data){
return (data[0].title == "Steve Jobs")
});
// add the quote(data[0].content) to my page.
$('.quote_text').html(data[0].content);
});
}
// get a new quote everytime i click the button (.message_btn)
$(document).ready(function(){
newQuote();
$('.message_btn').on('click',newQuote).fadeIn('slow');
});
发布于 2017-07-09 20:22:39
试试这段代码。服务器似乎正在使用Wordpress旧版API。我已经将请求URL更改为随机请求数组中的一个标题。
function newQuote() {
var titles = ["Steve Jobs", "Jonathan Ive", "Frank Zappa", "Kent Beck"];
var index = Math.floor(Math.random() * titles.length);
var url = "https://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[name]=" + titles[index];
$.getJSON(url, function(data) {
console.log("data length: " + data.length);
console.log("returned title: " + data[0].title);
if (data.length)
$('.quote_text').html(data[0].content + " - " + data[0].title);
});
}
https://stackoverflow.com/questions/44996035
复制相似问题