Javascript Ajax+;通知下拉框
我正在尝试实现通知下拉列表。 我使用的是ajax函数,它应该获取所有通知的json数据,并在不刷新页面的情况下更新通知的数量。 但是,该函数不起作用,我没有得到任何输出 我的路线:Javascript Ajax+;通知下拉框,javascript,python,jquery,html,flask,Javascript,Python,Jquery,Html,Flask,我正在尝试实现通知下拉列表。 我使用的是ajax函数,它应该获取所有通知的json数据,并在不刷新页面的情况下更新通知的数量。 但是,该函数不起作用,我没有得到任何输出 我的路线: @bp.route('/notifications') @login_required def notifications(): since = request.args.get('since', 0.0, type=float) notifications = current_user.notifi
@bp.route('/notifications')
@login_required
def notifications():
since = request.args.get('since', 0.0, type=float)
notifications = current_user.notifications.filter(
Notification.timestamp > since).order_by(Notification.timestamp.asc())
return jsonify([{
'name': n.name,
'data': n.get_data(),
'timestamp': n.timestamp
} for n in notifications])
和我的html页面:
{% extends 'bootstrap/base.html' %}
{% block navbar %}
<nav class="navbar navbar-default">
<div class="container">
<ul class="nav navbar-nav navbar-right">
{% if current_user.is_anonymous %}
<li>
<a role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Notifications {% set new_notifications = current_user.new_notifications() %}
<span id="message_count" class="badge"
style="visibility: {% if new_notifications %} visible
{% else %} hidden {% endif %};">
{{ new_notifications }}
</span>
</a>
<div class="dropdown-menu" aria-labelledby="dropdownMenuLink" id="dropdown">
<a class="dropdown-item" href="#">Action</a>
</div>
</li>
{% endif %}
</ul>
</div>
</nav>
<script>
function set_message_count(n) {
$('#message_count').text(n);
$('#message_count').css('visibility', n ? 'visible' : 'hidden');
}
{% if current_user.is_authenticated %}
$(function() {
var since = 0;
setInterval(function() {
$.ajax('{{ url_for('main.notifications') }}?since=' + since).done(
function(notifications) {
for (var i = 0; i < notifications.length; i++) {
if (notifications[i].name == 'unread_notifications_count'){
set_message_count(notifications[i].data);
since = notifications[i].timestamp;}
else {
let choice = document.createElement("a");
let body = document.createTextNode("this is me");
choice.appendChild(body);
dropdown.appendChild(choice);
}
}
}
);
}, 10000);
});
{% endif %}
</script>
{% endblock %}
{%extends'bootstrap/base.html%}
{%block navbar%}
{%如果当前_user.is_anonymous%}
-
{%endif%}
功能集\消息\计数(n){
$(“#消息计数”)。文本(n);
$('#message_count').css('visibility',n?'visible':'hidden');
}
{%如果当前用户是经过身份验证的%}
$(函数(){
var=0;
setInterval(函数(){
$.ajax('{url_for('main.notifications')}}?自='+since).done(
功能(通知){
for(var i=0;i
我哪里弄错了?
谢谢你的帮助 烧瓶路径示例:
@app.route('/example')
def example():
email = request.args.get('email', 404)
new_email = example_function(email)
return jsonify(res=new_email)
JS部分:
$.getJSON('/example', {
email: "example@example.com"
}, function (data) {
//data that flask returned
});