javascript - 如何在 React.js Web 应用程序(前端)中捕获来自远程 URL 的 JSON 数据
问题描述
我们尝试使用 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
它没有按预期工作
用于理解问题的示例图像:-
请帮助这些人,或者让我知道这是实现它的另一种方式。
谢谢
解决方案
推荐阅读
- mysql - 无法在 Netbeans 8.2 中启动/连接到 MySQL 8.0
- jquery - 使用引导程序和 python 路由导航选项卡
- c# - 使用 FillWeight 后如何获取 datagridview 列的绝对宽度?
- winforms - 如何对齐各个列中的输入数据?
- python - How to add leading 0's to different values in same column in pandas?
- node.js - 使用 jwt 和 nodejs 的密码重置链接
- c++ - 使用 scanf() 具有不同数据类型的多个输入
- sql - 如何为 SQL 连接子句合并我的数据
- laravel - Laravel, where, or where in main table 一个数据透视表
- blender - Blender 2.8:对象枢轴+像素对齐+对象部分分组(新手)