0

I am trying to submit a form via AJAX, however get a 400 error response from the server.

I think this is because the script doesn't present the data in an acceptable format, so have tried serialising the form however I still get a 400 error.

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; });
alias51
  • 8,178
  • 22
  • 94
  • 166
  • 1
    personally i don't think you need to use `if (data.success == true) {` because the success method already defines it successful. – Rafael Jan 01 '15 at 23:00
  • Just a random suggestion, try `$('form#profile-settings-form')` to select the form. Also, why do you prefix some variables with `$`? – Wouter Florijn Jan 01 '15 at 23:05
  • check this to see if it helps http://stackoverflow.com/questions/12221707/getting-a-400-error-on-ajax-call-with-json – pecci Jan 01 '15 at 23:41

0 Answers0