3

Although I've specified the content type, XMLHttpRequest keeps sending the data in multipart/form-data:

var xhr = new XMLHttpRequest();
xhr.open('POST', 'url', true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function(e) {
  //..........

//..........
xhr.send(new FormData("my_form"));

because in Chrome dev tools I see this:

------WebKitFormBoundaryfdsfdsfdsfds
Content-Disposition: form-data; name="name1"

something1
------WebKitFormBoundaryfdsfdsfdsfds
Content-Disposition: form-data; name="name2"

something2
------WebKitFormBoundaryfdsfdsfdsfds
Content-Disposition: form-data; name="name3"

something3

which is multipart/form-data

No jquery.

Mook
  • 33
  • 3
  • does [Sending POST data with a XMLHttpRequest](http://stackoverflow.com/questions/9713058/sending-post-data-with-a-xmlhttprequest) help? – Jaromanda X Dec 01 '15 at 10:29
  • @JaromandaX, no because I use FormData. – Mook Dec 01 '15 at 10:31
  • well then read [Using FormData objects](https://developer.mozilla.org/en/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest#Using_FormData_objects) - which states that `The transmitted data is in the same format that the form's submit() method would use to send the data if the form's encoding type were set to "multipart/form-data"` – Jaromanda X Dec 01 '15 at 10:32
  • @JaromandaX, in my form encoding type isn't set to "multipart/form-data" – Mook Dec 01 '15 at 10:36
  • 1
    @Mook — *would use to send the data **if** the form's encoding type **were** set to [that]* – Quentin Dec 01 '15 at 10:37
  • 1
    did you read the second link? did you read Quentin's answer? ... using `FormData` results in `Content-Type = multipart/form-data` - there's no way around it – Jaromanda X Dec 01 '15 at 10:37

1 Answers1

2

From the spec:

FormData
Let the request entity body be the result of running the multipart/form-data encoding algorithm with data as form data set and with utf-8 as the explicit character encoding.

Consequently FormData will always encode data using the multipart encoding. There is nothing you can do about that other than to construct your x-www-form-urlencoded string with your own code (i.e. to not use FormData).

Quentin
  • 800,325
  • 104
  • 1,079
  • 1,205