AJAX/JSON POST 请求不处理 HTML FLASK 请求

时间:2021-07-19 15:28:34

标签: javascript python html json flask

我正在尝试更新网页,以便在按下删除按钮时从表中删除附加行,并将 message_uid 值发送到 Flask 路由,以便我可以处理请求以实际更改消息中的状态数据库。但是请求不会发送。我可以删除该行并尝试使用表单进行更新,但需要删除函数不返回任何其他值,而不是在 HTML 中编辑更新部分。

路线

@bp.route("/delete", methods=['GET','POST'])
def delete():
    data = request.get_json() 
    data=delete(data, g.user)
    return Response(dumps(data), mimetype="application/json")

inbox.html

<h1><b>Messages: </b></h1><div id="update">  </div>  
     <div class="table-responsive"
 <table class="table table-bordered table-hover table-striped">
          <tr>
            <th></th>
            <th>Status</th>
            <th>Actions</th>
            <th>Date</th>
            <th>From</th>
            <th>To</th>
            <th>Subject</th>
            <th>Body</th>
            <th>References</th>
          </tr>

 {% for m in messages %}
          {%if m.status!="deleted"%}
            <tr > 
              <td><a class="btn btn-light" id="view" onClick="window.open('{{url_for('open_messages',message_id=m.message_uid)}}','View','resizable,height=500,width=500'); return false;" >View</a>
              <input type="hidden" name="message_uid" value={{m.message_uid}} id="message_uid" /> <input type="submit" class="btn-sm btn-secondary" value="Delete" /></td>

              <td>{{m.date}}</td>
              <td>{{m.sender}}</td>
              <td>{{m.recipient_username}}</td>

              <td>{{m.subject}}</td>
              <td>{{m.body}}</td>
              <td>{{m.references}}</td>
            </tr>
            {%endif%}
          {% endfor %}
      
           </table>
   
            <script>
        $(document).on("click",".delete", function(e) {
                e.preventDefault()
                var id = $(".message_uid").val();
                delete_id(id);
                $(this).parents("tr").remove();
            });

     function delete_id(id) {
                data = {
                    "id": id
                }
                console.log(data)
                $.ajax({
                    type:"POST",
                    url: "/delete",
                    contentType: "application/json",
                    data: JSON.stringify(data),
                    success: function(data){
                        //displays dates if set
                        $("#update").html("Message Deleted");
                        console.log(data)
                    }
                });

            };
            </script>

数据库处理

def delete(message_uid, user):
    text="MATCH (m:MESSAGE) WHERE m.UID='{message_uid}' SET m.{user}_status='deleted' RETURN m".format(message_uid=message_uid, user=user)
    verify=neo_execute(text)
    return verify

0 个答案:

没有答案