当个请求参数时,可以写为
fetch(url, {
method: 'POST',
body: JSON.stringify({
planID: val,
}),
})
.then(res => res.json())
.then((response) => {
if (response.code === '0') {
console.log(response.code);
}
});
多个请求体时,写为
const formData = new FormData();
// 将"ID=1&signal=value"形式封装成FormData形式
formData.append("Id", '1');
formData.append("signal", 'value');
const opts = {
method: 'POST',
body: formData,
};
fetch(`http://${url}/3d/devSync`, opts)
.then(res => res.json())
.then((response) => {
console.log(response);
});
携带额外请求头时:
fetch(this.api, {
method: this.key,
body: JSON.stringify(this.param),
headers: {
'Content-Type': 'application/json;charset=UTF-8',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': '*',
'Access-Control-Allow-Methods': 'GET,PUT,POST,DELETE,OPTIONS',
tk: localStorage.getItem('tk'),
},
}).then(res => res.json())
.then((response) => {
if (response) {
}
});
总结
原来传参方式有很多种形式
1、playload形式传参
const opts = {
method: 'POST',
body: JSON.stringfy({
planID:val,
}),
};
fetch(`http://${url}/3d/devSync`, opts);
2、formData形式
const formdata = new FormData();
formdata.append('planID', val);
const opts = {
method: 'POST',
body: formdata,
};
fetch(`http://${url}/3d/devSync`, opts);