fetch(url, {
method: 'post',
headers: {
"Content-type":
},
body: 'bar= foo& lorem=ipsum'
})
应该是吗?
A)
application/x-www-form-urlencoded; charset=UTF-8
B)
text/html; charset=utf-8
C)
application/json; charset=UTF-8
D)
Content-Type: multipart/form-data; boundry=something
我刚刚开始学习 JavaScript 和 API。在查看 MDN 文档时,我发现标头中有不同的选项。只是困惑该使用哪一个?
由
key=value
分隔的 &
参数序列是参数的 URL 编码格式。所以A是正确答案。
如果您发送 url 编码数据(ex:
key=value& key=value
),则 Content-Type
将是:
如果您发送 html 数据(ex:
<h1>hello</h1>
),则 Content-Type
将是:
如果您发送 json 数据(ex:
{"name" : "anyName"}
),则 Content-Type
将是:
如果您发送表单数据+文件(例如:
new FormData(document.getElementById('form1'))
)那么您不需要手动设置Content-Type
,即: