The content-Type in the Http header indicates the media Type information in the request and response, which is used to tell the server how to process the request data and the client how to parse the response data. Here are some of the content-Type attributes commonly used in the front end and how data is uploaded

application/x-www-form-urlencoded

In this submission mode, the data is represented as key-value pairs, which are joined by = and all key-value pairs are concatenated by &, as in

var obj = {
  a: 1.b: 2.c: 3
}
// obj is converted to the following string when the data is submitted
// ${post_url}? a=1&b=2&c=3
Copy the code

When submitting data in this way, encode and then string concatenation are required for non-alphanumeric data

application/json

The submitted data is a standard JSON object, which is converted into a string when the data is submitted. Such as

var obj = {
  a: 1.b: 2.c: 3
}
var objStr = JSON.stringify(obj)
Copy the code

You don’t have to worry about how complex the format is when you submit the data this way, just make sure that the final data is a standard JSON string.

multipart/form-data

Form-data is used to upload files in binary mode.

var formData = new FormData()
formData.append(key, value)
// Add the key name and value to the FormData object using the append method, and finally upload the object to the background
Copy the code

Here are some less commonly used content-Type attribute values

  • Text/HTML: HTML format
  • Text /plain: plain text format
  • Text/XML: indicates the XML format
  • Image/GIF: indicates the GIF image format
  • Image/JPEG: JPG format
  • Image/PNG: indicates the PNG image format
  • Application/XHTML + XML: XHTML format
  • Application/XML: XML data format
  • Application/Atom + XML: Atom XML aggregation format
  • Application/PDF: in PDF format
  • Application/MSWORD: Word document format
  • Application /octet-stream: binary stream data (e.g. common file downloads)