gpt4 book ai didi

json - ReactJS - 从 URL 获取 json 对象数据

转载 作者:行者123 更新时间:2023-12-03 13:20:55 25 4
gpt4 key购买 nike

如何将数据从 URL 获取到 ReactJS 中。

URL 的类型如下: http://www.domain.com/api/json/x/a/search.php?s=category

如果在浏览器中输入,将显示一个 json 对象。

如何将其加载到 ReactJS。

首先,我是这样开始的:

const dUrl = "http://www.the....";

console.log(dUrl);

但显然它显示的是网址而不是内容(我将能够过滤内容 - 这只是将其加载到我不知道的对象中的初始步骤)

编辑:我不想使用 jQuery。

最佳答案

尝试使用 Fetch API,这是 Facebook 推荐的。您应该避免将 jQuery 与 ReactJS 混合在一起,并坚持使用 ReactJS 操作 DOM 的方式。

function getMoviesFromApiAsync() {
return fetch('https://facebook.github.io/react-native/movies.json')
.then((response) => response.json())
.then((responseJson) => {
return responseJson.movies;
})
.catch((error) => {
console.error(error);
});
}

或者使用异步/等待

async function getMoviesFromApi() {
try {
let response = await fetch('https://facebook.github.io/react-native/movies.json');
let responseJson = await response.json();
return responseJson.movies;
} catch(error) {
console.error(error);
}
}

https://facebook.github.io/react-native/docs/network.html

我知道该 URL 用于 React Native 的文档,但这也适用于 ReactJS。

关于json - ReactJS - 从 URL 获取 json 对象数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39019094/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com