我该如何解决React中的Bad Request错误400?

时间:2020-03-15 20:00:01

标签: spring-boot

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

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

}

但是,当我发布来自ReactJS的请求时,它显示为 400错误的请求

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);
    }

0 个答案:

没有答案