Javascript 检查数据是否已发布在reactJS中
这里有新的反应。我现在正试图通过一个API来完成一个post,方法是遵循前面的堆栈溢出post。这是我的代码:Javascript 检查数据是否已发布在reactJS中,javascript,reactjs,post,Javascript,Reactjs,Post,这里有新的反应。我现在正试图通过一个API来完成一个post,方法是遵循前面的堆栈溢出post。这是我的代码: submitHandler = (event) => { const state = this.state.newClient this.setState({creating: false, submitable:false}); this.resetNewClient(); event.preventDefault(); const f
submitHandler = (event) => {
const state = this.state.newClient
this.setState({creating: false, submitable:false});
this.resetNewClient();
event.preventDefault();
const formData = new FormData();
formData.append('slug', state.slug);
formData.append('name', state.name);
formData.append('description', state.description);
return fetch('https://reqres.in/api/users', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then((json) => {
console.log('success')
})
}
如果是错误的,请更正我的代码。submitHandler由一个按钮触发,newClient是一个包含slug、name和description的数组。然而,除了我的代码是否正确之外,我想知道的是如何检查数据是否正确地发布在API中。如果有必要,这里是获取数据的componentDidMount
componentDidMount() {
fetch("https://reqres.in/api/users?page=3")
.then(data => data.json())
.then(datum => {
const results = datum.data.map(x => {
return {
slug: x.id,
name: x.first_name,
description: x.last_name
}
})
this.setState({ clients: results });
});
}
如果有更好的方法做到这一点,我也想知道。谢谢我通常会构造一个JavaScript对象,并使用JSON.stringify发布数据
const postData = {
slug: state.slug,
name: state.name,
description: state.description
};
return fetch('https://reqres.in/api/users', {
method: 'POST',
body: JSON.stringify(postData)
})
我的主要问题是如何检查,但这确实看起来更好,更容易理解。打开开发人员工具,检查网络面板是否发送了请求及其响应?