我想从标签中获取图像列表,但没有任何东西正在被控制台记录或渲染出来。不知道如果我缺少api参数。Flickr API不使用React和Axios获取图像
CODEPEN:http://codepen.io/anon/pen/PWgBgd?editors=0010
const url = "https://api.flickr.com/services/feeds/photos_public.gne?tags=kitten&format=json"
class App extends React.Component {
constructor() {
super()
this.state = {
items: []
}
}
componentWillMount() {
axios.get(url)
.then((response) => {
console.log(response.data.jsonFlickrFeed.items);
this.setState({
items: response.data.jsonFlickrFeed.items
})
})
.catch((err) => {
})
}
render() {
const mappedStorage = this.state.items.map((item) => <li>{item.media.m} </li>)
return (
<div>
hello
<ul>{mappedStorage}</ul>
</div>
);
}
}
ReactDOM.render(<App />, document.body)
您是否在控制台中看到任何CORS警告?你有没有试过控制台日志记录响应,看看返回什么 – finalfreq
我能够使用这个相同的代码,并得到它的工作,但我不得不使用这个插件:https://chrome.google.com/webstore/detail/allow-control -allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl = zh由于您使用客户端进行api调用,因此您需要确保允许跨源资源共享。浏览器通常不允许这样做,但它们是插件。最好的方法是让后端服务器发出请求,然后将其发送回前端,前端只与后端相同的来源 – finalfreq
我已经安装了扩展。这是一个codepen。仍然没有骰子。 http://codepen.io/anon/pen/PWgBgd?editors=0010 – user992731