Ich versuche, JSON in JSON-Struktur anfügen, [], aber ich habe eine sehr seltsame Situation. Ich habe diesen Code mit verschiedenen Datenbankprozeduren getestet und es hat funktioniert. Allerdings, wenn ich meinen Code verwende, funktioniert es nicht, obwohl alle anderen Dinge gleich sind und ich das Ergebnis aus der Datenbank bekomme.Flask JSON Append funktioniert nicht
Der Einfachheit halber entfernte ich unnötige Teile.
Script-Code für beide sind
<script>
$(function(){
$.ajax({
url: '/getAllWishes' or '/getAllMovies',
type: 'GET',
success:function(response) {
console.log(response);
},
error:function(error){
console.log(error);
}
});
})
</script>
Arbeitscode ist
@app.route('/getAllWishes')
def getAllWishes():
try:
print('test')
conn = mysql.connect()
cursor = conn.cursor()
cursor.callproc('sp_GetAllWishes',(3,))
result = cursor.fetchall()
print('result')
print(result)
wishes_dict = []
for wish in result:
wish_dict = {
'Id': wish[0],
'Title': wish[1],
'Description': wish[2],
'FilePath': wish[3],
'Like':wish[4],
'HasLiked':wish[5]
# 'Id': '1',
# 'Title': '1',
# 'Description': '1',
# 'FilePath': '1',
# 'Like':'1',
# 'HasLiked':'1'
}
wishes_dict.append(wish_dict)
# print(wishes_dict)
return json.dumps(wishes_dict)
#return json.dumps('[]')
except Exception as e:
return render_template('error.html',error = str(e))
und seine gedruckte Ergebnis ist
((4, u'Test', u'test', u'static/Uploads/667ca948-7ab0-4045-9b53-9646ece56336.jpg
', 1, 1), (5, u'wow', u'wow', u'static/Uploads/ddcf2c6a-7c03-4a97-850f-5f60fe246
f90.jpg', 0, 0), (6, u'ww', u'ww', u'static/Uploads/15b361bd-2d4d-4bb9-84d2-5ca9
1b15a0e6.jpg', 0, 0), (7, u'aa', u'aa', u'static/Uploads/36e2b946-5969-4587-84ed
-d8c1aab6dd6a.jpg', 0, 0), (8, u'ss', u'ss', u'static/Uploads/a35114be-8143-4113
-aff3-5de1fd89a464.jpg', 0, 0), (9, u'dd', u'dd', u'static/Uploads/a675a843-c158
-4cde-bb0e-86b44665dd4f.jpg', 0, 0), (10, u'ff', u'ff', u'static/Uploads/111cb65
0-0469-45cf-98d4-b0d035ad616c.jpg', 0, 0), (11, u'aaa', u'aaa', None, None, 0))
und sein Ergebnis von der Konsole in Browser
[{"Description": "test", "FilePath": "static/Uploads/667ca948-7ab0-4045-9b53-9646ece56336.jpg", "HasLiked": 1, "Id": 4, "Like": 1, "Title": "Test"}, {"Description": "wow", "FilePath": "static/Uploads/ddcf2c6a-7c03-4a97-850f-5f60fe246f90.jpg", "HasLiked": 0, "Id": 5, "Like": 0, "Title": "wow"}, {"Description": "ww", "FilePath": "static/Uploads/15b361bd-2d4d-4bb9-84d2-5ca91b15a0e6.jpg", "HasLiked": 0, "Id": 6, "Like": 0, "Title": "ww"}, {"Description": "aa", "FilePath": "static/Uploads/36e2b946-5969-4587-84ed-d8c1aab6dd6a.jpg", "HasLiked": 0, "Id": 7, "Like": 0, "Title": "aa"}, {"Description": "ss", "FilePath": "static/Uploads/a35114be-8143-4113-aff3-5de1fd89a464.jpg", "HasLiked": 0, "Id": 8, "Like": 0, "Title": "ss"}, {"Description": "dd", "FilePath": "static/Uploads/a675a843-c158-4cde-bb0e-86b44665dd4f.jpg", "HasLiked": 0, "Id": 9, "Like": 0, "Title": "dd"}, {"Description": "ff", "FilePath": "static/Uploads/111cb650-0469-45cf-98d4-b0d035ad616c.jpg", "HasLiked": 0, "Id": 10, "Like": 0, "Title": "ff"}, {"Description": "aaa", "FilePath": null, "HasLiked": 0, "Id": 11, "Like": null, "Title": "aaa"}]
und nicht funktionierender Code ist
@app.route('/getAllMovies')
def getAllMovies():
print('getAllMovies-------------------------')
try:
conn = mysql.connect()
cursor = conn.cursor()
cursor.callproc('sp_GetAllMovies')
result = cursor.fetchall()
print('result')
print(result)
movies_dict = []
print(movies_dict)
for movie in result:
movies_dict = {
# 'title': movie[0],
# 'mtype': movie[1],
# 'rating': movie[2],
# 'myear': movie[3],
# 'imagepath': movie[4]
'title': '1',
'mtype': '1',
'rating': '1',
'myear': '1',
'imagepath': '1'
}
print('movies_dict')
print(movies_dict)
movies_dict.append(movies_dict)
print('movies_dict after append')
print(movies_dict)
return json.dumps(movies_dict)
#return json.dumps('[]')
except Exception as e:
return render_template('error.html',error = str(e))
und sein gedruckte Ergebnis ist
((4, u'Test', u'test', u'static/Uploads/667ca948-7ab0-4045-9b53-9646ece56336.jpg
', 1, 1), (5, u'wow', u'wow', u'static/Uploads/ddcf2c6a-7c03-4a97-850f-5f60fe246
f90.jpg', 0, 0), (6, u'ww', u'ww', u'static/Uploads/15b361bd-2d4d-4bb9-84d2-5ca9
1b15a0e6.jpg', 0, 0), (7, u'aa', u'aa', u'static/Uploads/36e2b946-5969-4587-84ed
-d8c1aab6dd6a.jpg', 0, 0), (8, u'ss', u'ss', u'static/Uploads/a35114be-8143-4113
-aff3-5de1fd89a464.jpg', 0, 0), (9, u'dd', u'dd', u'static/Uploads/a675a843-c158
-4cde-bb0e-86b44665dd4f.jpg', 0, 0), (10, u'ff', u'ff', u'static/Uploads/111cb65
0-0469-45cf-98d4-b0d035ad616c.jpg', 0, 0), (11, u'aaa', u'aaa', None, None, 0))
und es wird den Fehler, wenn "movies_dict.append (movies_dict)" -Linie zu begehen.
Ich verstehe nicht wirklich, warum es nicht funktioniert. Ich habe diesen Teil schon länger als einen Tag getestet. Bitte helfen .. Danke.
@Momin Hallo verwenden. Ich denke, mein Code ist minimal. Ich versuche, Daten von MySQL zu lesen, Daten in das Wörterbuch einzugeben und das Ergebnis von Javascript unter Verwendung von Ajax zu erhalten. Ich schätze jedoch Ihren Kommentar. –