{% block add_js %}
<!--add_js_666-->
<script>
$.ajax({
type: "get",
dateType: "json",
url: "/api/v1/post/recent",
//async: false,
complete: function () {
$("#load").hide();
},
success: function (msg) {
$.each(msg,function (i,n) {
$("#writing ul").append(
'<li class="post-item"><div class="meta">' +
'<time datetime="'+ n[0].date +'" itemprop="datePublished">'+ n[0].date.substr(5,12) +'</time></div>'
+ '<span><a class="" href="/post/'+ n[1] +'">' + n[0].title + '</a></span></li>'
);
})
},
error: function () {
alert("ERROR");
}
});
</script>
{% endblock %}
# json
{
"2016-09-09-xxx": [
{
"author": "66666",
"date": "Mon, 11 Sep 2017 00:00:00 GMT",
"desc": "test page",
"tags": "test1 blog1",
"title": "test1"
},
"2016-09-09-xxx"
],
"2017-09-05-xxx": [
{
"author": "6666",
"date": "Sun, 10 Sep 2017 00:00:00 GMT",
"desc": "test page",
"tags": "test22 blog",
"title": "test3"
},
"2017-09-05-xxx"
]
}
后端处理简单调整测试了一下
@api.route('/api/v1/post/<name>')
def api_post(name):
if name == 'all':
return jsonify({key.path: {key.meta, key.path} for key in post.get_posts_list()})
elif name == 'recent':
return jsonify({key.path: [key.meta, key.path] for key in post.recent_post()})
else:
return redirect(url_for('api.api_index'))
你写的有点混乱,而且貌似基本不懂 js 还有 jq
这是jq的遍历操作
ajax success 的回调函数中 msg 是 json 数据,所以需要先转化成 object