我们尝试使用componentdidmount,这里我们可以使用Get请求从API提取数据并在控制台上打印。但是这里我们的要求不是要从API获取数据,我们要捕获来自远程URL remoteurlexample.com的数据并在控制台上打印这些值。
我们尝试过的:-
import React,{ Component } from "react";
import axios from 'axios'
class PostList extends Component{
constructor(props) {
super(props)
this.state={
posts: []
}
}
componentDidMount() {
axios.get('remoteurlexample.com',{ 'headers' : { 'Access-Control-Allow-Origin' : '*'}})
.then(response => {
console.log(JSON.stringify(response))
})
.catch(error => {
console.log(error)
})
}
render() {
return (
<div>
List of posts
</div>
)
}
}
export default PostList
它没有按预期工作
用于理解问题的示例图像:-
请对此家伙提供帮助,或者让我知道是实现它的另一种方法。
谢谢