Help us understand the problem. What is going on with this article?

javascriptのFormDataは、ajaxで飛ばすまで中身を見ることができないっぽい。

More than 1 year has passed since last update.

javascriptのメソッド、FormDataを使った時に
値が入らない???となって困ったのでシェアします。

結果的には入っていました

messages.js
  $('.message_form').on('submit', function(e) {
    e.preventDefault();
    var group_id = $('.group_id').val();

    var fd = new FormData($('.message_form').get(0))
    fd.append('body', $('.form').val())
    // $('.form').val() => "aaa"
    console.log(fd);
    // ここではfdを見ると FormData = {} となり、一見何も入っていないように見える
    $.ajax({
      type: 'POST',
      url: '/groups/' + group_id + '/messages.json',
      data: fd,
      processData: false,
      contentType: false,
      dataType: 'json'
    })
    .done(function(data) {
      console.log(data.body);
      // => "aaa"
      // なのに、ここで見るとfd.appendの部分で入れたbodyを見ることができる。
      data.name = $('.current_user_name').val();
      $('div.right__content').append(build_message(data));
      textField.val('');
    })
    .fail(function(data) {
      alert('送信に失敗しました。');
    });
  });
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away