从API通过JSON数组进行循环,可以使用jQuery获得所有结果或无结果,循环没有运气



我只是在尝试一些新的东西,以使技能更与时俱进,我对这个jQuery API响应有点不知所措。我把所有的结果都放在控制台上,我把它们显示在一个html文件上,但它们都是一体的。我想在每个单词之间留出一个间隔(这是[Words API][1],我被卡住了。我通常使用PHP,但好吧,这对我的计划来说并不实用。下面是代码,我将展示我在结果中所能做到的最好。谢谢!

<div id="div1">
// This is where it shows up as "unmitigated,butcherly,crimson,gory,homicidal,internecine"        
</div>
<script>  
const settings = {
"async": true,
"crossDomain": true,
"url": "https://wordsapiv1.p.rapidapi.com/words/bloody/similarTo",
"method": "GET",
"headers": {
"x-rapidapi-key": "<my key>",
"x-rapidapi-host": "<host>"
}
};
$.ajax(settings).done(function (response) {
console.log(response);
$("#div1").html(response.similarTo + " "); // This puts a comma between each word, but no spacing, when I want a line break. I need to be able to customize results, make them links, add styles, etc.
});

非常感谢!只有3周的严格";"待在家里";Covid19订单。我学到了很多!

我猜response.similarTo中有一个文本字符串,其中包含一堆逗号分隔的单词。

你可以这样处理它们。斯普利特用逗号分隔它们。此代码的其余部分将它们格式化为HTML列表并显示。

const results = []
const words = response.similarTo.split(',')
for (const word of words) {
/* do what you will with each word in turn, for example... */
results.push('<li>' + word + '</li>')
}
$("#div1").html('<ol>' + results.join('') + '</ol>')

最新更新