so, i just learn about make some website with flask. everything was fine until this error makes me crazy. can u solve my problem?
this is my def function to delete some note
JavaScript
x
11
11
1
@views.route('/delete-note',methods=['POST'])
2
def delete_note():
3
note = json.loads(request.data)
4
noteId = note['noteId']
5
note = Note.query.get(noteId)
6
if note:
7
if note.user_id == current_user.id:
8
db.session.delete(noteId)
9
db.session.commit()
10
return jsonify({})
11
and this is my .js code
JavaScript
1
9
1
function deleteNote(noteId) {
2
fetch("/delete-note", {
3
method: "POST",
4
body: JSON.stringify({ noteId: noteId }),
5
}).then((_res) => {
6
window.location.href = "/";
7
});
8
}
9
and this is how i make the button with html
JavaScript
1
11
11
1
<ul class="list-group list-group-flush" id="notes">
2
{% for note in user.notes %}
3
<li class="list-group-item">
4
{{ note.data }}
5
<button type="button" class="close" onClick="deleteNote({{ note.id }})">
6
<span aria-hidden="true">×</span>
7
</button>
8
</li>
9
{% endfor %}
10
</ul>
11
can u help me? i dont know how to solve it. please help me
Advertisement
Answer
Are you loading the js file correctly. You can try loading the function before the HTML markup eg
JavaScript
1
25
25
1
<head>
2
<script>
3
function deleteNote(noteId) {
4
fetch("/delete-note", {
5
method: "POST",
6
body: JSON.stringify({ noteId: noteId }),
7
}).then((_res) => {
8
window.location.href = "/";
9
});
10
}</script>
11
</head>
12
13
<body>
14
<ul class="list-group list-group-flush" id="notes">
15
{% for note in user.notes %}
16
<li class="list-group-item">
17
{{ note.data }}
18
<button type="button" class="close" onClick="deleteNote({{ note.id }})">
19
<span aria-hidden="true">×</span>
20
</button>
21
</li>
22
{% endfor %}
23
</ul>
24
</body>
25