客户端(React/axios.post)无法发布到服务器端 api(Golang/gin),状态代码为 404。我想让这个发布请求成功。
如下curl成功将数据写入mysql表中
curl -X POST -H "Content-Type: application/json" -d '{"title":"bbb", "content":"bbb"}' localhost:4000/api/post
但是,如果使用 axios.post,则会出现 404 错误。
这是目标源代码。
interface ArticleState {
title: string;
content: string;
redirect: boolean;
}
class Post extends React.Component<{}, ArticleState> {
constructor(props: {}) {
super(props);
this.state = {
title: '',
content: '',
redirect: false,
};
this.handleChangeTitle = this.handleChangeTitle.bind(this);
this.handleChangeContent = this.handleChangeContent.bind(this);
this.setRedirect = this.setRedirect.bind(this);
this.renderRedirect = this.renderRedirect.bind(this);
}
handleChangeTitle(e: React.FormEvent<HTMLInputElement>) {
this.setState({title: e.currentTarget.value});
}
handleChangeContent(e: React.FormEvent<HTMLInputElement>) {
this.setState({content: e.currentTarget.value});
}
setRedirect() {
this.setState({
redirect: true,
});
const data = {title: this.state.title, content: this.state.content};
axios.post('http://localhost:4000/api/post', data).then(res => {
console.log(res);
});
}
renderRedirect = () => {
if (this.state.redirect) {
return <Redirect to="/post/finish" />;
}
};
render() {
return (
<Container text style={{marginTop: '3em'}}>
<Form onSubmit={this.setRedirect}>
<Form.Input
label="Title"
name="title"
value={this.state.title}
onChange={this.handleChangeTitle}
/>
<Form.Field
label="Content"
name="content"
value={this.state.content}
control="textarea"
onChange={this.handleChangeContent}
/>
{this.renderRedirect()}
<Form.Button content="Submit" />
</Form>
</Container>
);
}
}
神不在的星期二
慕码人8056858
翻阅古今
相关分类