Flask,Ajax,获取Ajax网址的网址中的值

时间:2018-09-19 10:16:09

标签: javascript jquery

在python中,我有

@app.route('/update_account/<account_id>', methods=['POST'])
def update_account(account_id):

以html格式,我有:

<form id="update_account_form" name="update_account_form" action="{{ url_for('update_account', account_id=account._id) | safe }}" method="POST">

URL: http://<domain>/edit_account/5b9fbe55fb6fc072da02e2f6

在AJAX中,我有:

  $(function () {
  $('#update_account_form').submit(function (event) {
    event.preventDefault();
    account_id = 5b9fbe55fb6fc072da02e2f6
    $.ajax({
      url: '/update_account/'+account_id,
      data: $('#update_account_form').serialize(),
      type: 'POST',
      success: function (response) {

我的问题是,如何从URL中读取5b9fbe55fb6fc072da02e2f6的值以在我的AJAX调用中使用它。 我是否使用某种jQuery regex系统读取URL并计算出ID,或者是否可以通过ajax将html URL中的id传递给服务器端flask?

1 个答案:

答案 0 :(得分:1)

获取网址后使用split

$(function () {
      $('#update_account_form').submit(function (event) {
        event.preventDefault();
        var url = window.location.href;
        account_id = url.split("/").pop();
         ...........