2

I'm trying to make an AJAX file uploading using MinifiedJS and $.request.

I have and HTML file with an input file.

<input type="file" id="fileAvatar" />

And my Javascript code looks like this.

$('#fileAvatar').on('change', uploadAvatar);

function uploadAvatar(event) {
  var fData = new FormData();
  // Add files
  _.each(event.target.files, function(file, key) {
    fData.append(key, file);
  });
  // Add ohter parameters
  fData.append('user', 267345);

  // POST formData
  $.request(
    'post', 
    '/ajax/upload_avatar.php', 
    fData, 
    {'Content-Type': 'multipart/form-data'}
  ).then(function(response){
    console.log(response);
  });
}

I can see the POST to /ajax/upload_avatar.php on Chrome Dev tools, but i don't get any parameters on my PHP, it looks like the form data object is not sent.

Any thoughts?

1 Answers1

0

request()'s 4th argument is not a list of headers, but a settings object. If you want to specify a Content-Type, you'd need to use the headers setting:

$.request(
    'post', 
    '/ajax/upload_avatar.php', 
    fData, 
    {headers: {'Content-Type': 'multipart/form-data'}}
  )
Tim Jansen
  • 3,330
  • 2
  • 23
  • 28
  • Still the same problem i don't receive anything on server side. And now after make this change the headers on Chrome Dev Tools looks like an object `headers:[object Object]` instead of `Content-Type:application/x-www-form-urlencoded` as before. – Moisés Belchín Oct 09 '14 at 07:27