简体   繁体   English

来自React组件的REST调用

[英]REST calls from a react component

I'm trying to replicate same code here with different JSON, but the data is not loading. 我正在尝试使用不同的JSON 在此处复制相同的代码,但数据未加载。

Please help, I'm not sure what is missing in the code. 请帮忙,我不确定代码中缺少什么。

 import React from 'react'; export default class ItemLister extends React.Component { constructor() { super(); this.state = { items: [] }; } componentDidMount() { fetch('http://media.astropublications.com.my/api/drebar_landing.json') .then(result=>result.json()) .then(items=>this.setState({items})); } render() { return( <ul> {this.state.items.length ? this.state.items.map(item=><li key={item.id}>{item.Title}</li>) : <li>Loading...</li> } </ul> ) } } 

Your api response contains an object ArticleObject and the ArticleObject has array of objects so you need to set the items.ArticleObject to the state. 您的api响应包含一个对象ArticleObject,而ArticleObject具有对象数组,因此您需要将items.ArticleObject设置为状态。

Take a look at below solution for better understanding 请看下面的解决方案以更好地了解

componentDidMount() {
  fetch('http://media.astropublications.com.my/api/drebar_landing.json')
      .then(result=>result.json())
      .then(items=>this.setState({items:items.ArticleObject}));
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM