ReactJS-从URL获取JSON对象数据 [英] ReactJS - get json object data from an URL
问题描述
如何从URL将数据获取到ReactJS.
How do I get data from an URL into ReactJS.
URL具有以下类型: http://www.domain.com/api/json /x/a/search.php?s=category
The url is of the following type: http://www.domain.com/api/json/x/a/search.php?s=category
如果在浏览器中输入,将显示一个json对象.
which, if typed in a browser, will display a json object.
如何将其加载到ReactJS.
How do I load it to ReactJS.
首先,我以:
const dUrl = "http://www.the....";
console.log(dUrl);
但是显然它显示的是url而不是内容(我可以过滤它-这只是将其加载到我不知道的对象中的第一步)
but obviously it displays the url not the content (which, I will be able to filter - it's just this initial step of loading it into an object that I don't know)
我宁愿不使用jQuery.
I'd rather not use jQuery.
推荐答案
使用访存进行API调用.
Use fetch for making API calls.
fetch(http://www.example.com/api/json/x/a/search.php?s=category)
.then(response => response.json())
.then((jsonData) => {
// jsonData is parsed json object received from url
console.log(jsonData)
})
.catch((error) => {
// handle your errors here
console.error(error)
})
所有现代浏览器中都可以获取.
Fetch is available in all modern browsers.
这篇关于ReactJS-从URL获取JSON对象数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!