英文:
Using js/flask, getting internal 500 error on using Post request
问题
我有一个正在进行的小项目,涉及将文本区域的值发送到我的Flask应用的Python端,我的前端代码如下:
<input id="promptArea" type="text-area" placeholder="write your story..." />
<button id="submitButton">Make your story now!</button>
<p>your story here...</p>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$('#submitButton').click(function (event) {
event.preventDefault();
$('#submitButton').prop("disabled", true);
$.ajax({
data : { prompt : document.getElementById("promptArea").value },
type : 'POST',
url : '/prompt', //将文本发送到Flask端点以保存
success: function (data) { console.log('Sent Successfully') },
error: function (e) { console.log('Submission failed...') }
});
});
})
</script>
我基于这里进行了参考,我对Flask和全栈应用相对新手,但我在JavaScript端遇到了500错误,我该如何修复?
Python端(如果需要的话):
@app.route('/prompt', methods=['POST', 'GET'])
def prompt():
prompt = request.form['prompt'] # 解析接收到的内容到变量
chatbot.chat(prompt)
return ("works", 205) # 或者您希望返回的任何内容
英文:
So I have a little project Im working on, that involves sending the text-area value, to the python side of my flask app, and my front end code looks like this
<input id="promptArea" type="text-area" placeholder="write your story..." />
<button id="submitButton">Make your story now!</button>
<p>your story here...</p>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$('#submitButton').click(function (event) {
event.preventDefault();
$('#submitButton').prop("disabled", true);
$.ajax({
data : { prompt : document.getElementById("promptArea").value },
type : 'POST',
url : '/prompt', //post grabbed text to flask endpoint for saving role
success: function (data) { console.log('Sent Successfully') },
error: function (e) { console.log('Submission failed...') }
});
});
})
</script>
I based this off here and I'm relatively new to flask and full stack apps, and I'm getting an error 500 on the JavaScript end, How do I fix this?
python side(if required)
@app.route('/prompt', methods=['POST', 'GET'])
def prompt():
prompt = request.form['prompt'] #parse received content to variable
chatbot.chat(prompt)
return ("works", 205) #or whatever you wish to return
答案1
得分: 0
在检查服务器日志后,我发现只需添加:
from flask import Flask, request
英文:
After checking server logs, I discovered I just had to add
from flask import Flask, request
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论