首页 > 解决方案 > 如何在 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

标签: reactjsapirestrequestfetch-api

解决方案


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
},

推荐阅读