【发布时间】:2020-03-06 16:34:12
【问题描述】:
我正在尝试从 rest api 检索数据。我正在尝试记录结果,但没有任何结果,到目前为止我的代码是:
反应
import React, { Component } from 'react';
import { render } from 'react-dom';
import Hello from './Hello';
import './style.css';
class App extends Component {
constructor() {
super();
this.state = {
name: 'React',
awsApiData: [],
};
}
componentDidMount() {
console.log('app mounted');
fetch('https://nc1fi4y2i7.execute-api.eu-west-2.amazonaws.com/api/data')
.then(data => data.json())
.then(data => this.setState({awsApiData: data.home}, () => console.log(data.home)))
}
render() {
return (
<div>
<p>
Start editing to see some magic happen :)
</p>
</div>
);
}
}
render(<App />, document.getElementById('root'));
api 架构
{
"home": [
{
"title": "John Doe title",
"body": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s.",
"image": "image/example.jpg"
}
],
"about": [
{
"title": "John is the main part 1",
"body": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s.",
"image": "image/example.jpg"
}
]
}
有什么想法吗?
【问题讨论】:
-
API 可能失败。也可以执行 .catch 来获取承诺。
-
另外,如果有的话,从你的控制台添加日志
-
控制台中没有任何记录
标签: javascript reactjs api