【发布时间】:2020-08-24 11:23:09
【问题描述】:
在 reactjs 中获取 api 数据时,我得到一个空页面作为输出。
回购:https://github.com/te3t0/building-small-blog
我可能是 reactjs 的新手,我得到一个空页面,即使数据被完美地获取。如果有人能在我做错的地方帮助我,那就太好了。非常感谢您。
endpoint_url : http://localhost:8000/api/blog_list
api 数据:
[
{
"id": 1,
"url": "http://localhost:8000/api/blog_detail/brown",
"title": "brown",
"slug": "brown",
"image": "http://localhost:8000/media/blog/image_2.jpg",
"description": "",
"created_on": "2020-05-08T15:20:53Z",
"status": true,
"category": [
1
]
},
{
"id": 2,
"url": "http://localhost:8000/api/blog_detail/black",
"title": "black",
"slug": "black",
"image": "http://localhost:8000/media/blog/loc.png",
"description": "",
"created_on": "2020-05-08T17:14:31Z",
"status": true,
"category": [
2
]
}
]
./src/Base.js
export default class App extends Component{
state = {
bloglist:[]
};
componentDidMount(){
this.fetchData()
}
fetchData = async () => {
try{
const response = await fetch("http://localhost:8000/api/blog_list");
const jsonResponse = await response.json()
this.setState({bloglist:jsonResponse})
}
catch(error){
console.log(error)
}
}
render(){
const {bloglist} = this.state
if(!bloglist){
return (
<div>
<h1>loading...</h1>
</div>
)
}
return (
{
bloglist.map(bloglist => (
<h3 class="mb-2">
<a href="single.html">{bloglist.title}</a>
</h3>
<p class="mb-4">{bloglist.description}</p>
))
}
)
}
}
【问题讨论】:
-
添加指向您的存储库的链接
-
“空白页”是什么意思?
-
这是我的存储库--> github.com/te3t0/building-small-blog@DenisTsoi
-
通常我在获取数据时得到一个空白页。我没有得到预期的结果。 @GalAbra
-
应用程序正在获得
GET http://localhost:8000/api/blog_list net::ERR_FAILED- 来自Access to fetch at 'http://localhost:8000/api/blog_list' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
标签: javascript django reactjs