我无法获取数据表单API。我尝试了不同的方法,但是没有运气。这是我的代码和API。我想通过react fetch方法使用这些数据(JSON)。
// My Fetch API
fetchData() {
fetch('MYAPI_URL')
.then(response => response.json())
// .then(parsedJSON => console.log(parsedJSON)) //prints the data on console
// .then(parsedJSON => console.log(parsedJSON.status)) // Not Worked
// .then(parsedJSON => console.log(parsedJSON.data.Products) ) // Not Worked
.then(parsedJSON => console.log(parsedJSON.status.data) ) // not working.
.then(parsedJSON => parsedJSON.data.map(pro => (
{
..... Other code ....
}
))
}
// below is my api data
"status": true,
"data": {
"banner": "mydomain.com/img/img1.png",
"Products": [
{
"CategoryCode": "03",
"CategoryName": "Appetizers",
"SubCategory": [
{
"SubCategoryCode": "038",
"SubCategoryName": "Chicken Wings",
"Image": "banner": "mydomain.com/img/img1.png",
"SubCategoryType": [
{
"TypeCode": "05",
"TypeName": "Normal",
"AdditionalPrice": "0",
"TypeSize": [
{
"SizeCode": "17",
"SizeName": "6 Pcs",
"Description": "Oven baked hot and spicy chicken wings.",
"Price": "349",
"Toppings": [
]
}
]
}
]
...............
// I want to get Products Code etc. But I can't. No idea where I am doing wrong
当我使用此提取API时,它正在工作。数据与以上数据几乎相同,但这不是真正的Rest API。
答案 0 :(得分:0)
如果您只需要接收数据而不更新React组件(不以本地状态存储),则可以查看以下答案:https://stackoverflow.com/a/44644532/1263904
否则,请检查以下内容:https://stackoverflow.com/a/50980558/1263904