Я новичок в Python и Flask и пытаюсь написать программу Cootie Catcher. Я посмотрел на несколько учебных пособий и проверяю то, что дал мне Google AI, пока не пойму, что происходит. Всякий раз, когда я нажимаю кнопку «Отправить в своей форме», я получаю следующую ошибку: < /p>
Неподдерживаемый тип носителя
не пытался загрузить данные json, потому что тип контента запроса не был «приложением /json '. class = "Snippet-Code">
Fortune Teller
Fortune Teller
red
yellow
blue
green
1
2
3
4
submit
function getFortune(choiceType, choice) {
// connects getFortune js function to get_function python method
fetch('/fortune', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({choiceType: choiceType, choice: choice}),
})
// format response
.then(response => response.json())
// return response to window
.then(data => {
document.getElementById('fortune').textContent = data.fortune;
});
}
< /code>
< /div>
< /div>
< /p>
и вот мой файл Python: < /p>
from flask import Flask, render_template, request, jsonify
import random
app = Flask(__name__)
fortunes = {
"colors": {
"red": ["RA", "RB"],
"yellow": ["YA", "YB"],
"blue": ["BA", "BB"],
"green": ["GA", "GB"],
},
"numbers": {
"1": ["1A", "1B"],
"2": ["2A", "2B"],
"3": ["3A", "3B"],
"4": ["4A", "4B"],
}
}
@app.route('/')
def default():
return render_template('fortune.html')
@app.route('/fortune',methods = ['GET', 'POST'])
def get_fortune():
data = request.get_json()
choice_type = data['choiceType']
choice = data['choice']
if choice_type == 'number':
fortune_list = fortunes['numbers'].get(choice, ["Try again"])
elif choice_type == 'color':
fortune_list = fortunes['colors'].get(choice, ["Try again"])
else:
fortune_list = "Invalid choice."
fortune = random.choice(fortune_list)
return jsonify({'fortune': fortune})
if __name__ == '__main__':
app.run(debug = True)
< /code>
Если кто -то может сломать, что такое подвеска, я бы очень признателен. Цените ваше время.
Подробнее здесь: https://stackoverflow.com/questions/795 ... media-type
Flask Html Cootie atcher - 415 неподдерживаемый тип мультимедиа ⇐ Html
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение