前提
ここに質問の内容を詳しく書いてください。
(例)
TypeScriptで●●なシステムを作っています。
■■な機能を実装中に以下のエラーメッセージが発生しました。
実現したいこと
プログラミング初心者です。
jsでjson形式に変換しajaxとflaskを使ってpythonに送りpython側で辞書形式に変換し返すものを実装中にエラーが発生しました。
json_str = json.loads(data)の部分を書かないで送られてきたデータをそのまま返すようにするとエラーが出ないのでajaxでの通信はうまくいっていると思うのですが、json_str = json.loads(data)を書いたとたんエラーが出ます。
ここに実現したいことを箇条書きで書いてください。
- ▲▲機能を動作するようにする
発生している問題・エラーメッセージ
エラーメッセージ jquery-3.6.1.min.js:2 POST http://127.0.0.1:5000/test 500 (INTERNAL SERVER ERROR) jquery-3.6.1.min.js:2 POST http://127.0.0.1:5000/test 500 (INTERNAL SERVER ERROR)
該当のソースコード
python
import random import json array = random.sample(range(1,54),53) def speed(data): if data == "first": return array[0:4] else: json_str = json.loads(data) return json_str
該当のソースコード
js ソースコード $(document).ready(function(){ json_str = "first"; json = JSON.stringify(json_str); $.ajax ({ url: '/test', type: 'post', data: json, contentType: "application/json", }).done(function(data) { console.log(data) $(".me-card-number-1").attr({ "alt": data[0] }); $(".me-card-number-2").attr({ "alt": data[1] }); $(".me-card-number-3").attr({ "alt": data[2] }); $(".me-card-number-4").attr({ "alt": data[3] }); }).fail(function() { alert("失敗") }); }); var count = 4; $(".me-card-number-1").on("click",function() { let str = $(this).attr("class"); count += 1; let data = {"str":str,"count":count}; console.log(count); json = JSON.stringify(data); console.log(json) $.ajax({ url: '/test', type: 'post', data: json, dataType:"json", contentType: "application/json", }).done(function(data) { console.log(data); $(".me-card-number-1").attr({ "alt": data }); }).fail(function() { alert("失敗"); }); }); $(".me-card-number-2").on("click",function() { const str = $(this).attr("class"); count += 1; let json_array = {"str":str,"count":count}; console.log(str) json = JSON.stringify(json_array); $.ajax({ url: '/test', type: 'post', data: json, contentType: "application/json", }).done(function(data) { console.log(data) $(".me-card-number-2").attr({ "alt": data }); }).fail(function() { alert("失敗"); }); }); $(".me-card-number-3").on("click",function() { const str = $(this).attr("class"); count += 1; var json_array = {"str":str,"count":count}; console.log(str) json = JSON.stringify(json_array); $.ajax({ url: '/test', type: 'post', data: json, contentType: "application/json", }).done(function(data) { console.log(data) $(".me-card-number-3").attr({ "alt": data }); }).fail(function() { alert("失敗"); }); }); $(".me-card-number-4").on("click",function() { const str = $(this).attr("class"); count += 1; var json_array = {"str":str,"count":count}; console.log(str); json = JSON.stringify(json_array); $.ajax({ url: '/test', type: 'post', data: json, contentType: "application/json", }).done(function(data) { console.log(data) $(".me-card-number-4").attr({ "alt": data }); }).fail(function() { alert("失敗"); }); });
app.py ソースコード from flask import Flask from flask import request from flask import render_template import json import main app = Flask(__name__) #ページが開かれたときindex.htmlを表示させる @app.route("/") def index(): return render_template( "index.html" ) @app.route('/test', methods=['POST']) def test(): if request.method == 'POST': data = request.json #https://python.keicode.com/lang/functions-call-function-defined-in-another-file.php#1 #送られてきたデータをmain.pyのspeed関数に渡す main_data = main.speed(data) return main_data # webサーバーの立ち上げ if __name__ == "__main__": app.run(debug=False)
試したこと
ここに問題に対して試したことを記載してください。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/09/27 00:34
2022/09/27 00:52
2022/09/27 01:09
2022/09/27 01:24
2022/09/27 01:51