gpt4 book ai didi

javascript - 显示来自 json 的帖子

转载 作者:行者123 更新时间:2023-11-29 16:33:23 25 4
gpt4 key购买 nike

我必须显示来自 json 的帖子,但是当我尝试使用 for 循环时什么也没有发生。这是我的代码,我做错了什么。是的

JS

   var root = 'https://jsonplaceholder.typicode.com';
$.ajax({
url: root,
method: 'GET',
success: function(response) {
console.log(response);

jQuery.get('/posts', function(posts) {
for (var i = 0; i < posts.length; i++) {
document.write(posts[i]);
}
});
}
});

有什么想法吗?

最佳答案

var root = 'https://jsonplaceholder.typicode.com';
$.ajax({
url: root+"/posts",
method: 'GET',
contentType: 'application/json',
success: function(posts) {
console.log("Data=>",posts);
$.each(posts,function(index,post){
document.write("<p>"+post.title+"</p>");
});
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>

您应该提供正确的 URL root+'/posts' 以便从 API 获取数据。

关于javascript - 显示来自 json 的帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53742706/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com