0

I found this answer explaining how to use a FormData object to upload a file over ajax. I'm wondering if it's possible to rearrange the data structure that's sent over in the POST request.

My backend is in rails, and the controller expects the data like this:

xmlFile: {
  some_property: 1,
  attachment: [the file]
}

However, if I use a solution similar to the one from the above link, something like

var data = new FormData();
data.append('some_property', id);
data.append('source', file);

(where file is a File object), and then submit that with $.ajax

$.ajax({
  url: url,
  data: data,
  processData: false,
  type: 'POST',
  contentType: 'multipart/form-data', 
  mimeType: 'multipart/form-data'
});

the form submits, but the data isn't namespaced under xmlFile. If I try to do something like

$.ajax({
  url: url,
  data: {xmlFile: data},
  processData: false,
  type: 'POST',
  contentType: 'multipart/form-data', 
  mimeType: 'multipart/form-data'
});

it doesn't work. The request data just shows [object Object].

Community
  • 1
  • 1
Sam Selikoff
  • 12,366
  • 13
  • 58
  • 104

0 Answers0