1. Content-Type field : The server mainly automatically obtains the encoding used by the request body according to the Content-Type attribute in the request header, and then parses the message body.
2. Four common contentTypes
1.aplication/x-www-form-urlencoded
: The default attribute when the form form does not specify enctype. When submitting data, it is encoded in key = val & key1 = val1
the
Note 1 : For JQuery's Ajax, the default value of Content-Type is application/x-www-form-urlencoded;charset=utf-8
as follows:
/**
* 带file的表单提交
*/
$("#cms-file-submit").click(function(){
var data = $("#cms-form").serializeArray();
var fm = new FormData()
$(data).each(function(i){
fm.append(this.name, this.value);
});
fm.append('file', $("#file")[0].files[0]);
// console.log(fm);
// 将获取到的数据post给服务器
url = SCOPE.save_url;
var jump_url = "";
$.ajax(
{
url:url,
type: 'POST',
data: fm,
dataType:'JSON',
cache: false,
contentType: false,
processData: false,
success:function(){
//1.成功的处理
alert(1);
}
}
);
});
So we need to set when using $.ajax()
contentType: false
: (default: "application/x-www-form-urlencoded") the content encoding type when sending information to the server.
processData: false
:(default: true) By default, the data option is passed The incoming data, if it is an object (technically speaking, as long as it is not a string), will be processed and converted into a query string to match the default content type application/x-www-form-urlencoded
.
Reason: The data we give is the data that has been encoded with FormData, and does not require jQuery to perform string conversion.
Note 2: JQuery uses utf-8 encoding, so pay attention to this problem when doing gbk encoding.
2.multipart/form-data
: When we use the form to upload files, we must make the enctyped of the form equal to this value.
3.application/json
: Tell the server that the body of the message is a serialized JSON string. Can support complex structured data.
4.text/xml