【发布时间】:2018-09-09 09:38:48
【问题描述】:
我的 JSON 看起来像这样
{
"towns" :[
{
"id" : 0,
"location": "Japanifornia",
"status" : 1
},
{
"id" : 1,
"location" : "Kohonohakatsuki",
"status" : 2
},
{
"id" : 2,
"location" : "Taxis",
"status" : 5
}
]
}
我在 React 中使用 axios 时出现问题。它应该返回这样的结果。
[
{
"id": 0,
"location": "Japanifornia",
"status": 1
},
{
"id": 1,
"location": "Kohonohakatsuki",
"status": 2
},
{
"id": 2,
"location": "Taxis",
"status": 5
}
]
问题发生了,我在 React 的 componentWillMount() 中使用 axios 从 localhost:7777/towns 获取请求。 但是从未发出过请求,我检查了来自 json-server 的日志,当我加载或重新加载包含执行 GET 请求的组件的 localhost:3000 时没有发生请求。 我已经签入了从 Firefox 到节点控制台的其他应用程序。 所有应用程序都返回它应该在 React 旁边什么都不返回的东西
这是我的组件WillMount
componentWillMount() {
axios.get('127.0.0.1:7777/towns')
.then((res) => res.json())
.then((result) => this.setState({data:result,isLoading: false}))
}
【问题讨论】:
-
使用组件DidMount。同时检查网络选项卡并确保您正在渲染组件
-
@RIYAJKHAN 太糟糕了,没有任何改变。
-
检查你的 JS 控制台。如果未发出请求,则存在 js 问题。
-
在 axios 上获取 404 错误,在 fetch 上获取有效的 JSON 解析错误
标签: javascript reactjs axios