这是我使用ajax连接flask的java脚本,但它显示404错误。我不明白我的错误在哪里
<script>
function getBotResponse() {
var rawText = $("#textInput").val();
var userHtml = '<p class="userText"><span>' + rawText + '</span></p>';
$("#textInput").val("");
$("#chatbox").append(userHtml);
document.getElementById('userInput').scrollIntoView({block: 'start', behavior: 'smooth'});
$.ajax({
type: 'POST',
url: '/predicton',
dataType: 'json',
contentType: 'application/json; charset=utf-8'
}).done(function(data) {
var botHtml = '<p class="botText"><span>' + data + '</span></p>';
$("#chatbox").append(botHtml);
document.getElementById('userInput').scrollIntoView({block: 'start', behavior: 'smooth'});
});
}
$("#textInput").keypress(function(e) {
if(e.which == 13) {
getBotResponse();
}
});
$("#buttonInput").click(function() {
getBotResponse();
})
</script>
烧瓶脚本连接上面的java脚本。我无法连接使用烧瓶json结果到java脚本。它无法连接,我想从烧瓶中取出数据的问题
# webapp
app = Flask(__name__, template_folder='./')
@app.route('/')
def index():
return render_template('index.html')
@app.route('/prediction', methods=['POST', 'GET'])
def prediction():
if request.form != None and 'message' in request.form:
msg = request.form['message']
response = pred(str(msg))
return jsonify(response)
else: # Through chatbot
#msg = request.args.get(['message'])
response = pred(str(request.get_json['message']))
return jsonify(response)
if __name__ == '__main__':
app.debug = True
app.run()
答案 0 :(得分:0)
您的脚本中存在拼写错误:
url: '/predicton'
必须是
url: '/prediction'
(在JS实现中)