New to react here. I'm getting this error: xhr.js:177 POST https://localhost:44355/api/people/addperson 400 and I can't figure out why. I checked all over StackOverflow, but couldn't find a good answer from similar questions.
On my page, I have 3 textboxes (first name, last name, age) and an ADD button to add a person to a table beneath the textboxes. The error occurs when I click the add button.
Here's my controller:
public class PeopleController : ControllerBase
{
private string _connectionString;
public PeopleController(IConfiguration configuration)
{
_connectionString = configuration.GetConnectionString("ConStr");
}
[HttpPost]
[Route("addperson")]
public void AddPerson(Person person)
{
var repo = new PeopleRepository(_connectionString);
repo.AddPerson(person);
}
}
Here's my component:
import React from 'react';
import AddEditPerson from './AddEditPerson';
import PersonRow from './PersonRow';
import axios from 'axios';
import { produce } from 'immer';
class PeopleTable extends React.Component {
state = {
people: [],
person: {
firstName: '',
lastName: '',
age :''
},
isAdd : true
}
componentDidMount = () => {
axios.get('/api/people/getpeople').then(response => {
this.setState({ people: response.data })
})
}
onAddClick = () => {
axios.post('/api/people/addperson', this.state.person).then(() => {
axios.get('/api/people/getpeople').then(response => {
const person = {
firstName: '',
lastName: '',
age:''
}
this.setState({ people: response.data, person})
})
})
}
}
//here I have a render function that shows a component with the textboxes
//and the onClick for the add button is the onAddClick function above.
.catchblock. You need to add one and print out the error to see what's happening - since it's a 400 there's likely an issue with the data you're sending, either missing values or the wrong shape or somethingError: Request failed with status code 400 at createError (createError.js:16) at settle (settle.js:17) at XMLHttpRequest.handleLoad (xhr.js:62)console.log(error.response). Also if you can edit backend code, you could add logging on the backend to try and see what the issue ishttps://localhost:44355is not you're front localhost port ?