【发布时间】:2019-04-30 00:52:58
【问题描述】:
尝试将 json 数据获取到前端并尝试了一堆版本的 axios 请求,但不断收到 404 状态码。
这是前端格式的一个例子:
class App extends Component {
constructor () {
super();
this.state = {
message: ''
};
this.handleClick = this.handleClick.bind(this);
}
handleClick () {
axios.get('./hello')
.then(response => {
this.setState({ message: response.data.text });
})
.catch(error => {
console.log(error);
});
}
render () {
return (
<div className="button-container">
<button className='button' onClick={this.handleClick}>Click Me</button>
<p>{this.state.message}</p>
</div>
)
}
}
和后端路由:
@app.route('/hello')
def hello_world():
return jsonify(text='hello world')
错误消息显示“加载资源失败:服务器响应状态为 404(未找到)”或显示 http://localhost:5003/hello 不存在
【问题讨论】:
-
您可以访问您提到的链接吗?本地主机