dataType
In AJAX request, the dataType parameter is mainly used to specify the expected number ofThe data type returned by the server. This parameter is commonly found in jQuery's AJAX method.
effect:
1. Tell AJAX how to parse requestsData returned by the server。
2. Affects the data format received by the success callback. If you set dataType:'json', jQuery willAutomatically parse response data into JavaScript objects, not the original string, so there is no need to manually.
3. Help jQuery set the correct Accept request header (in some cases). For example, dataType:'json' will add Accept:application/json to the request header
Compare Fetch API / Axios
The Fetch API does not have dataType, but uses .json(), .text(), and .blob() methods.
fetch('/data').then(response=>()) //Similar to dataType:'json' .then(data=>console,log(data))
Axios will automatically resolve based on Content-Type, but it can also force the responseType
('/api',{responseType:'json'}) //similar dataType
Content-Type
Content-Type is one of the most important headers in HTTP requests and responses, and it is used to specifyTypes of data transmitted in a request or response, let the server or client know how to correctly parse the data.
effect:
1. Tell the server client what format of data it sends
2. Tell the client server what format of data it returns
3. Ensure that the data can be parsed and processed correctly
//In the request (client -> server)Content-Type:'application/json' // means that the client sends data in JSON format// In response (server -> client)Content-Type:'text/html;charset=UTF-8' //Indicates that the server returns yesUTF-8EncodedHTMLdocument
Common Content-Type values:
type | illustrate | Typical uses |
application/json | JSON string | API Request/Response |
application/x-www-form-urlencoded | URL encoded form data | HTML form submission |
multipart/form-data | Contains form data uploaded by file | File upload |
text/html | HTML Documentation | Web page return |
text/plain | Plain text | Simple text data |
text/css | css stylesheet | Style File |
text/javascript | JavaScript Code | JS Files |
image/png | PNG picture | Picture files |
Notes:
1. Get requests usually do not need to set Content-Type, because GET requests generally do not have a request body.
2. The content-Type must be set correctly by post/put request, otherwise the server may not be able to parse the data correctly.
3. The browser has security restrictions on certain Content-Types (such as cross-domain requests)
4. Character encoding can be attached to Content-Type: text/html; charset=UTF-8
The difference between Content-Type and Accept:
Content-Type: DescriptionCurrently sentData type
Accept: DescriptionThe client wants to receiveData type
// The client sends form data, but wants the interface JSON format responseAccept:application/json Content-Type:application/x-www-form-urlencoded
This is the article about the detailed explanation of the functions of dataType and content-type parameters in jQuery Ajax. For more related dataType and content-type parameters, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!