foreword
Everyone knows that when the front-end interacts with the background, it is inevitable to pass parameters. Generally, params are used in get requests, but in post requests, we have two common ways to pass parameters: JSON object format and formData format, but some scenarios require us to convert these two data formats, such as form submission, some data are in JSON object format, some are in FormData format, and we need to convert them at this time.
Common POST data submission methods
1. application/x-www-form-urlencoded
Submit data in the form format form
through action
the attribute of the tag. If enctype
the attribute , application/x-www-form-urlencoded
the data will be submitted in the form of finally.
2. multipart/form-data
This is also a common way of submitting post
data . When we use the form to upload files, we must make form
the of enctype
equal to this value, which is mostly used for file uploads.
3. application/json
application/json
Everyone is familiar with this Content-Type
as a response header. Objectively speaking, more and more people now use it as a request header to tell the server that the message body is a serialized json
string. Due to the popularity of json
the specification , IE
all major browsers except the low version support natively JSON.stringify
, and the server-side language also has functions json
to json
, so you will not encounter any trouble when using , and json
the format supports more complex structures than key-value pairs data.
4. text/xml
In contrast json
, xml
it is not better for data exchange, it contains too much packaging, and it does not match the data model of most programming languages, it is xml
data-oriented, json
object-oriented and structure-oriented. At present, it is mostly used for XML
storing data, storing configuration files, etc. where structured storage is required.
The difference between JSON format and formData format
JSON request header:
JSON payload:
formData request header:
formData load:
From the above pictures, we can roughly understand that the front-end transmits binary data, and the two content-type
are different. json
We have already serialized, but formdata
still need to be processed.
Two formats of formdata
multipart/form-data
: It can not only upload binary data such as files, but also upload form key-value pairs, but it will be converted into a message in the end;x-www-form-urlencoded
: Only key-value pairs can be uploaded, and the key-value pairs are separated by intervals.
json object to formData
Convert one by one
let obj = {
id: "001",
name: "小蓝",
age: "18",
sex: "女",
};
const formData = new FormData();
formData.append("id", obj.id);
formData.append("name", obj.name);
formData.append("age", obj.age);
formData.append("sex", obj.sex);
console.log(formData);
All property value conversions
let obj = {
id: "001",
name: "小蓝",
age: "18",
sex: "女",
};
const formData = new FormData();
Object.keys(obj).map((key) => {
formData.append(key, obj[key]);
});
console.log(formData);
formData object to json
var jsonObj = {
};
formData.forEach((value, key) => (jsonObj[key] = value));