在React Js中从api提取数据失败

时间:2018-08-08 14:47:36

标签: reactjs api fetch postman fetch-api

我是react js的新手,我正尝试从My API中获取数据,我可以使用POSTMAN来获取结果,并显示数据 我的问题是当我在我的react js应用程序中使用链接:“ http://localhost:51492/api/user/1”时,数据无法显示... PS:je travail avec代码SandBox 这是我的代码,显示用户的所有关注者:

    import React from "react";
import ReactDOM from "react-dom";
import "antd/dist/antd.css";
import { List, Avatar, Button, Spin } from "antd";
import PropTypes from "prop-types";
import { withStyles } from "@material-ui/core/styles";

import reqwest from "reqwest";

const fakeDataUrl =
  "http://localhost:51492/api/follower/all/1";

class LoadMoreList extends React.Component {
  state = {
    loading: true,
    loadingMore: false,
    showLoadingMore: true,
    data: []
  };

  componentDidMount() {
    this.getData(res => {
      this.setState({
        loading: false,
        data: res.results
      });
    });
  }

  getData = callback => {
    reqwest({
      url: fakeDataUrl,
      type: "json",
      method: "get",
      contentType: "application/json",
      success: res => {
        callback(res);
      }
    });
  };

  onLoadMore = () => {
    this.setState({
      loadingMore: true
    });
    this.getData(res => {
      const data = this.state.data.concat(res.results);
      this.setState(
        {
          data,
          loadingMore: false
        },
        () => {
          // Resetting window's offsetTop so as to display react-virtualized demo underfloor.
          // In real scene, you can using public method of react-virtualized:
          // https://stackoverflow.com/questions/46700726/how-to-use-public-method-updateposition-of-react-virtualized
          window.dispatchEvent(new Event("resize"));
        }
      );
    });
  };

  render() {
    const { loading, loadingMore, showLoadingMore, data } = this.state;
    const loadMore = showLoadingMore ? (
      <div
        style={{
          textAlign: "center",
          marginTop: 12,
          height: 32,
          lineHeight: "32px"
        }}
      >
        {loadingMore && <Spin />}
        {!loadingMore && (
          <Button onClick={this.onLoadMore}>loading more</Button>
        )}
      </div>
    ) : null;
    return (
      <List
        style={{
          width: "50%",
          left: "25%"
        }}
        className="demo-loadmore-list"
        loading={loading}
        itemLayout="horizontal"
        loadMore={loadMore}
        dataSource={data}
        renderItem={item => (
          <List.Item
            actions={[
              <Button type="primary" icon="user-add">
                suivre
              </Button>,
              <a>Message</a>
            ]}
          >
            <List.Item.Meta
              avatar={
                <a>
                  <Avatar src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png" />{" "}
                </a>
              }
              title={<a href="https://ant.design">{item.userProfile}</a>}
             />
          </List.Item>
        )}
      />
    );
  }
}

LoadMoreList.propTypes = {
  classes: PropTypes.object.isRequired
};

export default withStyles()(LoadMoreList);

这是我输入URL时PostMan显示的内容:http://localhost:51492/api/follower/all/1

enter image description here

我认为缺少的是邮递员结果开头的“结果属性”,我认为一定是这样的: enter image description here

请帮助我,谢谢您的关注

0 个答案:

没有答案