jsonをJSONの構造体[]に追加しようとしていますが、私は非常に奇妙な状況に直面しています。 私はこのコードを別のデータベース手続きコードを使ってテストしました。しかし、コードを使用すると、残りのものはすべて同じですが、データベースから結果を得ることはできますが、機能しません。Flask JSON Appendが機能しない
簡単にするため、不要な部分は削除しました。
それらの両方のためのスクリプトコードは
<script>
$(function(){
$.ajax({
url: '/getAllWishes' or '/getAllMovies',
type: 'GET',
success:function(response) {
console.log(response);
},
error:function(error){
console.log(error);
}
});
})
</script>
作業をコードしている
@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))
であり、その印刷結果は
((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))
で、ブラウザでのコンソールからその結果が
です[{"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"}]
なく、作業コードが
@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))
であり、その印刷結果は
((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))
であり、 "movies_dict.append(movies_dict)" 行をコミットするときにエラーを取得します。
なぜ動作していないのか分かりません。私はすでにこの部分を1日以上テストしました。助けてください..ありがとう。
@Mominこんにちはようなものを使用することができます。私のコードは最小だと思う。私は、MySQLからデータを読み込み、辞書にデータを入力し、AJAXを使用してJavaScriptから結果を取得しようとしています。私はあなたのコメントに感謝します。 –