表单提交时出现Ajax 400错误

时间:2015-01-01 22:47:49

标签: jquery ajax

我正在尝试通过AJAX提交表单,但是从服务器获得400错误响应。

我认为这是因为脚本没有以可接受的格式显示数据,因此尝试将表格序列化,但仍然会出现400错误。

HTML:

   <form id="profile-settings-form" method="post" enctype="multipart/form-data">
      <div class="bio-messages"></div>
      <textarea name="bio" id="id_bio" rows="10" class="form-control"></textarea>
      <button type="submit" class="btn btn-primary refresh" id="submit-profile-settings">Save</button>
   </form>

SCRIPT:

var $profileForm = $('#profile-settings-form');
var $profileFormUrl = 'user_dashboard_profile_settings.php';
$profileForm.on('submit', function(e) {
e.preventDefault();
$.ajax({
  type: 'POST',
  url: $profileFormUrl,
  data: $profileForm.serialize(),
  dataType: 'json',
  success: function(data) {
    if (data.success == true) {
      msg = 'Form saved.'
      $profileForm.find('.bio-messages').html('<div class="alert alert-success"><h4>' + msg + '</h4></div>').show().delay(5000).fadeOut('slow'); } },
  error: function(data) {
    msg = 'The form contains some errors:';
    errors = ''
    $.each(data.form_errors, function(field, error) {
      errors = error });
    $profileForm.find('.bio-messages').html('<div class="alert alert-danger alert-dismissible" role="alert"><button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button><h4>' + msg + '</h4>' + errors + '</div>').show().delay(5000).fadeOut('slow'); }
    });

return false; });

0 个答案:

没有答案