Javascript I';我对如何从我的flask python api向AJAX表单发送数据感到困惑 函数getResponse(){ 让userText=$(“#textInput”).val(); 让userHtml=''+userText+''; $(“#textInput”).val(“”); $(“#聊天室”).append(userHtml); document.getElementById('userInput').scrollIntoView({block:'start',behavior:'smooth'}); $.get(“/get”,{msg:userText}).done(函数(数据){ var botHtml=''+data+''; $(“#聊天室”).append(botHtml); document.getElementById('userInput').scrollIntoView({block:'start',behavior:'smooth'}); }); } $(“#文本输入”)。按键(功能(e){ //如果按下回车键 如果(e.which==13){ getResponse(); } }); $(“#按钮输入”)。单击(函数(){ getResponse();

Javascript I';我对如何从我的flask python api向AJAX表单发送数据感到困惑 函数getResponse(){ 让userText=$(“#textInput”).val(); 让userHtml=''+userText+''; $(“#textInput”).val(“”); $(“#聊天室”).append(userHtml); document.getElementById('userInput').scrollIntoView({block:'start',behavior:'smooth'}); $.get(“/get”,{msg:userText}).done(函数(数据){ var botHtml=''+data+''; $(“#聊天室”).append(botHtml); document.getElementById('userInput').scrollIntoView({block:'start',behavior:'smooth'}); }); } $(“#文本输入”)。按键(功能(e){ //如果按下回车键 如果(e.which==13){ getResponse(); } }); $(“#按钮输入”)。单击(函数(){ getResponse();,javascript,python,ajax,flask,Javascript,Python,Ajax,Flask,我已经将训练聊天机器人的python代码导入到我的main.py文件中。我想对来自我网站的用户输入调用聊天功能,然后以ajax形式将聊天机器人响应发送回网站。我在这方面出了什么问题?当函数上的路径明显是时,您正在请求/get>/治疗师。您不能在特定网页上发出get请求吗?基本上,我想在“msg”数据上调用python chat(),然后在ajax中将chat()的响应设置为botHTML变量,然后将该数据发送回网页是的,但您正在进行“get”请求url/get。这就是问题所在。您的函数治疗师希望

我已经将训练聊天机器人的python代码导入到我的main.py文件中。我想对来自我网站的用户输入调用聊天功能,然后以ajax形式将聊天机器人响应发送回网站。我在这方面出了什么问题?

当函数上的路径明显是
时,您正在请求
/get
>/治疗师
。您不能在特定网页上发出get请求吗?基本上,我想在“msg”数据上调用python chat(),然后在ajax中将chat()的响应设置为botHTML变量,然后将该数据发送回网页是的,但您正在进行“get”请求url
/get
。这就是问题所在。您的函数
治疗师
希望(通过路由)被调用作为
/theraper
。您需要修复
$中的URL。get
呼叫。那么,我如何在治疗师页面上执行此get请求?这就是表单所在的位置。我是否需要在同一页面上具有其他功能?
@app.route("/therapist", methods=['GET', 'POST'])
def therapist():
    if request.method == 'POST':
        userText = request.args.get('msg')
        chat_response = chat(userText)
        return getResponse == chat_response
    return render_template("therapist.html")
<div id="userInput">
              <input id="textInput" class="form-control" type="text" name="msg" placeholder="Talk to me!">
              <input id="buttonInput" class="btn btn-success form-control" type="submit" value="Send">
          </div>
      </div>
    </div>

  <script>
    function getResponse() {
        let userText = $("#textInput").val();
        let userHtml = '<p class="userText"><span>' + userText + '</span></p>';
        $("#textInput").val("");
        $("#chatbox").append(userHtml);
        document.getElementById('userInput').scrollIntoView({block: 'start', behavior: 'smooth'});
        $.get("/get", { msg: userText }).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 enter key is pressed
        if(e.which == 13) {
            getResponse();
        }
    });
    $("#buttonInput").click(function() {
        getResponse();