Javascript 单击按钮从数据库中删除表行

Javascript 单击按钮从数据库中删除表行,javascript,jquery,html,ajax,flask,Javascript,Jquery,Html,Ajax,Flask,我有一张桌子: <table class="table" id="mytable"> <tr> <td>Column 1</td> <td>Column 2</td> <td><button type="submit" class="btn removeButton">Remove</button></td> </tr> </t

我有一张桌子:

<table class="table" id="mytable">
  <tr>
    <td>Column 1</td>
    <td>Column 2</td>
    <td><button type="submit" class="btn removeButton">Remove</button></td>
  </tr>
</table>
服务器端代码:

@app.route('/main', methods=['GET', 'POST', 'DELETE'])
def main():
  if request.method == 'POST':
    insert_in_db(request.form)
    return redirect(url_for('next_page'))

  elif request.method == 'DELETE':
    #Retrieve data from the request and remove it from the database

  return render_template('next_page.html')
在ajax请求中,如何从当前表行(点击按钮的旁边)发送数据,之后如何在服务器端检索数据?
我尝试调用request.data,但只得到一个空字符串。我在删除请求中尝试了request.form,这给了我ImmutableMultiDict([])。有什么建议吗?我目前正在使用Flask作为web框架。谢谢

要通过AJAX调用发送行数据-

$('#mytable').on('click', '.removeButton', function(events){
    var col1 = $(this).closest('tr').find('td').eq(0).html(); // or you could loop through the cells
    var col2 = $(this).closest('tr').find('td').eq(1).html();
    $(this).closest('tr').remove();
    $.ajax({
        type: "DELETE",
        data: {column1 : col1, column2 : col2}, // post values
        success:function(result){
            window.location.href = $SCRIPT_ROOT + '/main';
        }
    });
  });

这些应该在
请求中可用。args

创建
行时没有要序列化的数组。行应该是什么?在这个例子中,我想检索两个元素:列1和列2。因此,基本上是当前行中每列的数据。您必须创建一个数组,然后将每个值推送到数组中。好的,我可以这样做,但如何在服务器端检索它?request.data会给我数组吗?我更改了代码,以便将所有td元素附加到数组中,但在服务器端的request.data中仍然会得到一个空字符串。嗯,是的,但我没有使用PHP。您知道如何使用Python检索数据吗?(我在用烧瓶)对不起,我只是在自动打字。我已经更改了答案。我更新了代码,现在打印了request.args,但是我得到的不是数据ImmutableMultiDict([]),而是有效的调用:request.values返回:CombinedMultiDict([ImmutableMultiDict([]),ImmutableMultiDict([('column1',u'5'),('column2',u'1')])),因此为了只检索数据,我们应该调用request.form。我真的不明白为什么。。。
$('#mytable').on('click', '.removeButton', function(events){
    var col1 = $(this).closest('tr').find('td').eq(0).html(); // or you could loop through the cells
    var col2 = $(this).closest('tr').find('td').eq(1).html();
    $(this).closest('tr').remove();
    $.ajax({
        type: "DELETE",
        data: {column1 : col1, column2 : col2}, // post values
        success:function(result){
            window.location.href = $SCRIPT_ROOT + '/main';
        }
    });
  });