首页 > 解决方案 > 如何解决 React 中的错误请求错误 400?

问题描述

当我将此请求从 Postman 发送到 SpringBoot 时,它可以工作。

        "location": "New Jersey",
        "category": {
            "name": "Travel",
            "id": 1
        },
        "expensedate": "2020-03-17",
        "descript": "New York City"

}

但是,当我从 ReactJS 发布请求时,它显示400 Bad Request

const item1={
            descript : event.target.description.value,
            category:{
                 id:1, name:event.target.category.value
             },
            expensedate : event.target.date.value,
            location : event.target.location.value
        }
        console.log(event.target.category.key);
        console.log(item1)


        await fetch(`/api/expenses`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json; charset=utf-8'},
            body: JSON.stringify(item1)
        })

SpringBoot

@PostMapping("/expenses")
    Expense  createExpense(@RequestBody Expense expense){
        return this.expenseRepository.save(expense);
    }

标签: spring-boot

解决方案


推荐阅读