Ajax——不能将要发送的数据作为“;x-www-form-urlencoded”;

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

本文关键字:x-www-form-urlencoded 数据 不能 Ajax      更新时间:2023-09-26

尽管我已经指定了内容类型,但XMLHttpRequest仍在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"));

因为在Chrome开发工具中,我看到了以下内容:

------WebKitFormBoundaryfdsfdsfdsfds
Content-Disposition: form-data; name="name1"
something1
------WebKitFormBoundaryfdsfdsfdsfds
Content-Disposition: form-data; name="name2"
something2
------WebKitFormBoundaryfdsfdsfdsfds
Content-Disposition: form-data; name="name3"
something3

multipart/form-data

没有jquery。

来自规范:

FormData
让请求实体主体是运行多部分/表单数据编码算法的结果,其中数据作为表单数据集,utf-8作为显式字符编码。

因此,FormData始终使用多部分编码对数据进行编码。除了用自己的代码构造x-www-form-urlencoded字符串(即不使用FormData)之外,您对此无能为力。