使用ajax提交表单,但获得“非法调用”

时间:2018-08-16 04:43:00

标签: javascript html ajax html5 ajaxform

我尝试通过ajax提交表单,下面是表单。

<form class="form-vertical" method="POST" id="request-form" action="/post_handler?request=add_data" enctype="multipart/form-data">
  <div class="form-group">
    <label for="date_inp" class="control-label">Date</label>
      <input class="form-control hasDatepicker" id="datepicker" type="text" name="date">
    </div>
  </div>
  <div class="form-group">
    <label for="file_inp">Upload File</label>
    <div>
      <input class="form-control" id="file_inp" type="file" placeholder="Upload File" name="file">
    </div>
  </div>
  <div class="form-group">
    <div>
      <button type="submit" class="btn btn-default submit-button" onclick="on_click_form_submit(event);">Submit</button>
    </div>
  </div>
</form>

这是点击功能。

on_click_form_submit = function(event) {

    event.preventDefault();

    var form_data = new FormData($('#request-form')[0]),
        form_url = '/' + $('#request-form')[0].action.split('/').pop();

    console.log('url: ' + form_url);
    $.ajax({
            url: form_url,
            type: 'POST',
            data: form_data,
            dataType: 'json',
            encode: true
        })
        .done(function(response) {
            alert(response);
        })
        .fail(function(xhr, status, error) {
            alert(xhr.responseText);
        });

    return false;
};

当我单击“提交”时,它会报告

Uncaught TypeError: Illegal invocation
  at add (jquery-1.9.1.js:7340)
  at buildParams (jquery-1.9.1.js:7392)
  at Function.jQuery.param (jquery-1.9.1.js:7360)
  at Function.ajax (jquery-1.9.1.js:7863)
  at Object.on_click_form_submit (spa.shell.js:301)

1 个答案:

答案 0 :(得分:2)

Per the docs,jQuery的$.ajax接受:

  

类型:PlainObject或String或Array

因此,您的form_data应该是这些格式之一-不应是FormData的实例。这取决于您的后端期望什么,但是一种选择是使用serializeArray()将表单的值转换为对象:

on_click_form_submit = function(event) {

  event.preventDefault();

  var form_data = $('#request-form').serializeArray(),
    form_url = '/' + $('#request-form')[0].action.split('/').pop();

  console.log('url: ' + form_url);
  $.ajax({
      url: form_url,
      type: 'POST',
      data: form_data,
      dataType: 'json',
      encode: true
    })
    .done(function(response) {
      alert(response);
    })
    .fail(function(xhr, status, error) {
      alert(xhr.responseText);
    });

  return false;
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form class="form-vertical" method="POST" id="request-form" action="/post_handler?request=add_data" enctype="multipart/form-data">
  <div class="form-group">
    <label for="date_inp" class="control-label">Date</label>
    <input class="form-control hasDatepicker" id="datepicker" type="text" name="date">
  </div>
  </div>
  <div class="form-group">
    <label for="file_inp">Upload File</label>
    <div>
      <input class="form-control" id="file_inp" type="file" placeholder="Upload File" name="file">
    </div>
  </div>
  <div class="form-group">
    <div>
      <button type="submit" class="btn btn-default submit-button" onclick="on_click_form_submit(event);">Submit</button>
    </div>
  </div>
</form>