reactjs - 如何在 API 的 POST 请求正文中发送数组和布尔数据
问题描述
如何在 API 的 POST 请求正文中发送数组和布尔数据。以下是我试图发送数组和布尔数据但它被转换为字符串的代码。
fetch('https://cors-anywhere.herokuapp.com/http://35.153.16.107:8080/restaurant/createProfile',{
method:'post',
headers:{Authorization:localStorage.getItem('token'), 'Content-Type':'application/json'},
body: JSON.stringify({
route:this.state.route,
side:this.state.side,
mustHaveDishes:this.state.mustTryDishes, //This is array
preOrderDineIn:this.state.preOrderDineIn, //This is boolean
preOrderTakeaway:this.state.preOrderTakeaway, // This is boolean
veg:this.state.vegOnly // This is boolean
}),
})
.then(response => response.json())
.then(data =>{
if(data.statusCode === '2000'){
console.log(this.state.mobile);
this.props.onRouteChange('orderar','nav');
}
else
console.log(data.error);
})
这就是我要发送数组和布尔数据的方式-
mustHaveDishes:["paratha"],
preOrderDineIn:true,
preOrderTakeaway:true,
veg:true
解决方案
JSON.stringify
除了获取 JSON 对象并将其转换为字符串之外,您认为在做什么?
身体应该看起来像
body: {
route: this.state.route,
side: this.state.side,
mustHaveDishes: this.state.mustTryDishes,
preOrderDineIn: this.state.preOrderDineIn,
preOrderTakeaway: this.state.preOrderTakeaway,
veg: this.state.vegOnly
},
推荐阅读
- powerapps - 使用下拉列表中的选定项目在 Power Apps 中显示项目
- reactjs - 如果可以被 3 整除,如何根据条件为列表中的特定项目分配不同的背景颜色?
- excel - 将工作表从另一个打开的工作簿复制到当前工作簿
- asp.net-core - ASP.NET Core 中的 HttpContext.TraceIdentifier 有多独特?
- sendgrid - Sendgrid:如何向电子邮件模板添加动态链接?
- css - 我有一个围绕 svg 旋转但不固定的 SVG
- r - 更改 boxpoints 的颜色并在 plotly boxplot 中添加分布曲线
- mysql - 'While' 循环在设定的秒数后未退出
- r - 如何将保存的 Leaflet 小部件插入 Rmarkdown HTML 输出
- c++ - 使用替换密码进行加密