发布时间:2024-08-27 17:01
content-type: application/x-www-form-urlencoded;charset=utf-8
form-data: k1=v1&k2=v2
<form action="/" method="post" enctype="multipart/form-data">
<input type="text" name="name" value="some text">
<input type="file" name="fileName">
<button type="submit">Submit</button>
</form>
POST /foo HTTP/1.1
Content-Length: 68137
Content-Type: multipart/form-data;
boundary=---------------------------974767299852498929531610575
---------------------------974767299852498929531610575
Content-Disposition: form-data; name="name" value="some text"
---------------------------974767299852498929531610575
Content-Disposition: form-data; name="fileName"; filename="foo.txt"
Content-Type: text/plain
(content of the uploaded file foo.txt)
---------------------------974767299852498929531610575--
POST http://www.example.com HTTP/1.1
Content-Type: application/json;charset=utf-8
{"title":"test","sub":["a","b","c"]}
function submit2() {
var xhr = new XMLHttpRequest();
xhr.timeout = 2000;
var obj = {a: 1, b: 2};
xhr.open('POST', '/');
xhr.send(obj);
}
POST [http://www.example.com](http://www.example.com) HTTP/1.1
Content-Type: text/xml
<!--?xml version="1.0"?-->
<methodcall>
<methodname>method1</methodname>
<params>
<param>
<value>50</value>
</param>
</params>
</methodcall>
1.传统的ajax请求时候,
Content-Type
默认为"文本"类型。2.传统的form提交的时候,
Content-Type
默认为"Form"类型。
function submit3() {
var p1 = 'name=a&val=b';
var p1 = {name: a, val: b};
axios.post('/url', p1 )
}
当传递的是字符串的时候:application/x-www-form-urlencoded
当传递的是对象的时候:application/json
总结:
1.axios传递字符串的时候,
Content-Type
默认为"Form"类型。2.axios传递对象的时候,
Content-Type
默认为"JSON"类型