我正在使用react-redux和json server fake api创建CRUD应用,现在我可以添加,删除和编辑了,但是添加项目时遇到问题,出现错误。 Request failed with status code 404
这是我在sandobox中的实时应用:live demo
这是我添加用户的操作功能。
export const addNewUser = data => {
return dispatch => {
axios.post("http://localhost:3000/users/")
.then(response =>{
dispatch(addUser(data));
})
.catch(error => {
const errorMsg = error.message;
dispatch(fetchUsersFailure(errorMsg));
})
}
}
这是我要添加用户的表单
import React, {useState} from "react";
import { useDispatch } from "react-redux";
import { editUser, updateUser } from "../redux/acitons/users/Users";
function UserForm({ user }) {
const dispatch = useDispatch();
const [name, setName] = useState(user.name);
const handleSubmit = (e) => {
e.preventDefault()
console.log(name);
dispatch(updateUser({ ...user, name }));
};
const handleCancel = () => {
dispatch(editUser(user.id));
};
return (
<tr>
<td>{user.id}</td>
<td>
<input
defaultValue={user.name}
onChange={e => setName(e.target.value)}
/>
</td>
<td>
<button type="button" className="btn outline" onClick={handleCancel}>
<i className="material-icons">Cancel</i>
</button>
<button
type="button"
className="btn btn-success btn-link"
onClick={handleSubmit}
>
<i className="material-icons">save</i>
</button>
</td>
</tr>
);
}
export default UserForm;
我的代码有什么问题?任何帮助或建议将不胜感激