使用 js/flask,在使用 Post 请求时出现内部 500 错误

huangapple go评论73阅读模式
英文:

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

&lt;input id=&quot;promptArea&quot; type=&quot;text-area&quot; placeholder=&quot;write your story...&quot; /&gt;
&lt;button id=&quot;submitButton&quot;&gt;Make your story now!&lt;/button&gt;
&lt;p&gt;your story here...&lt;/p&gt;
    &lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js&quot;&gt;&lt;/script&gt;
        &lt;script&gt;
        $(document).ready(function() {
            $(&#39;#submitButton&#39;).click(function (event) {
            event.preventDefault();
            $(&#39;#submitButton&#39;).prop(&quot;disabled&quot;, true);
            $.ajax({
                 data : { prompt : document.getElementById(&quot;promptArea&quot;).value },
                 type : &#39;POST&#39;,
                 url : &#39;/prompt&#39;, //post grabbed text to flask endpoint for saving role
                 success: function (data) { console.log(&#39;Sent Successfully&#39;) },
                 error: function (e) { console.log(&#39;Submission failed...&#39;) }
            });
        });
        })
        &lt;/script&gt;

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(&#39;/prompt&#39;, methods=[&#39;POST&#39;, &#39;GET&#39;])
def prompt():
prompt = request.form[&#39;prompt&#39;] #parse received content to variable
chatbot.chat(prompt)
return (&quot;works&quot;, 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

huangapple
  • 本文由 发表于 2023年6月16日 11:05:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76486711.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定