Mook
Mook

Reputation: 33

Ajax -- cannot make the data to be sent as "x-www-form-urlencoded"

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.

Upvotes: 3

Views: 120

Answers (1)

Quentin
Quentin

Reputation: 943635

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).

Upvotes: 2

Related Questions