1 回答

TA贡献1856条经验 获得超5个赞
fetch还接受第二个参数,它是一个文字对象,包含与请求有关的信息——例如请求方法、模式、标头和 POST BODY。
直接来自 Mozilla 文档:
var data = { name: "justin", code: "SDF", type: "some-type" };
fetch(url, {
method: 'POST', // *GET, POST, PUT, DELETE, etc.
mode: 'cors', // no-cors, *cors, same-origin
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
credentials: 'same-origin', // include, *same-origin, omit
headers: {
'Content-Type': 'application/json'
// 'Content-Type': 'application/x-www-form-urlencoded',
},
body: JSON.stringify(data) // body data type must match "Content-Type" header
});
https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
添加回答
举报