无法在reactjs上使用axios获取数据

0ve6wy6x  于 2021-06-21  发布在  Mysql
关注(0)|答案(3)|浏览(318)

我是reactjs的新开发者,我开发了一个表,前台是reactjs,后台是nodejs,数据库是mysql。我想用select请求在表上获取一个数据,我用postaman在后端测试了它,效果很好。
我的前端:

class ListeClients extends Component {
  constructor(props) {
    super(props);
    this.state = {
      clients: []
    };
  }

  componentDidMount() {
    console.log("inside componentWillMount");
    let self = this;
    axios({
      method: "get",
      url: "/app/listeclients/",
      withCredentials: true,
      headers: {
        "Access-Control-Allow-Origin": "*",
        "Content-Type": "application/json",
        Accept: "application/json"
      }
    })
      .then(response => {
        // const clients = response.data;
        console.log(JSON.stringify(response.data));
        self.clients = response.data;
        //    this.setState({clients});
      })
      .catch(error => {
        console.log(error);
      });
  }

  render() {
    console.log(this.state.clients);
    return (
      <div className="animated fadeIn">
        <Row>
          <Col>
            <Card>
              <CardHeader>
                <h4>
                  <strong>
                    <i className="fa fa-align-justify" /> Tous les clients
                  </strong>
                </h4>
              </CardHeader>
              <CardBody>
                <div className="container">
                  <div className="panel panel-default p50 uth-panel">
                    <table className="table table-hover">
                      <thead>
                        <tr>
                          <th>Code</th>
                          <th>Prenom</th>
                          <th>Nom</th>
                          <th>Email</th>
                          <th>Telephone</th>
                          <th>Action</th>
                        </tr>
                      </thead>
                      <tbody>
                        {this.state.clients.map(client => (
                          <tr key={client.id}>
                            <td>{client.Code} </td>
                            <td>{client.Prenom}</td>
                            <td>{client.Nom}</td>
                            <td>{client.Email}</td>
                            <td>{client.Telephone}</td>
                            <td>
                              <a>Edit</a>|<a>Delete</a>
                            </td>
                          </tr>
                        ))}
                      </tbody>
                    </table>
                  </div>
                </div>
              </CardBody>
            </Card>
          </Col>
        </Row>
      </div>
    );
  }
}

export default ListeClients;

但当我在reactjs上运行时,我得到:

[]
length: 0__proto__: Array(0)concat: ƒ concat()constructor: ƒ Array()copyWithin: ƒ copyWithin()entries: ƒ entries()every: ƒ every()fill: ƒ fill()filter: ƒ filter()find: ƒ find()findIndex: ƒ findIndex()forEach: ƒ forEach()includes: ƒ includes()indexOf: ƒ indexOf()join: ƒ join()keys: ƒ keys()lastIndexOf: ƒ lastIndexOf()length: 0map: ƒ map()pop: ƒ pop()push: ƒ push()reduce: ƒ reduce()reduceRight: ƒ reduceRight()reverse: ƒ reverse()shift: ƒ shift()slice: ƒ slice()some: ƒ some()sort: ƒ sort()splice: ƒ splice()toLocaleString: ƒ toLocaleString()toString: ƒ toString()unshift: ƒ unshift()values: ƒ values()Symbol(Symbol.iterator): ƒ values()Symbol(Symbol.unscopables): {copyWithin: true, entries: true, fill: true, find: true, findIndex: true, …}__proto__: Object
ListeClients.js:15 inside componentWillMount

我想得到这个表上的数据:

请问怎么修?

cu6pst1q

cu6pst1q1#

尝试使用默认的axios get()方法:

axios.get("/app/listeclients/")
  .then(response => {
    // const clients = response.data;
    console.log(JSON.stringify(response.data));
    self.clients = response.data;
    //    this.setState({clients});
  })
  .catch(error => {
    console.log(error);
  });

默认情况下,它应该可以很好地处理json数据。

dbf7pr2w

dbf7pr2w2#

您的组件应该是这样的:

class ListeClients extends Component {
  constructor(props) {
    super(props);
    this.state = {
      clients: []
    };
  }

  componentDidMount() {
    axios({
      method: "get",
      url: "/app/listeclients/",
      withCredentials: true,
      headers: {
        "Access-Control-Allow-Origin": "*",
        "Content-Type": "application/json",
        Accept: "application/json"
      }
    })
      .then(response => {
        if (response && response.data) {
          this.setState({ clients: response.data });
        }
      })
      .catch(error => console.log(error));
  }

  render() {
    let { clients } = this.state;
    return (
      <div className="animated fadeIn">
        <Row>
          <Col>
            <Card>
              <CardHeader>
                <h4>
                  <strong>
                    <i className="fa fa-align-justify" /> Tous les clients
                  </strong>
                </h4>
              </CardHeader>
              <CardBody>
                <div className="container">
                  <div className="panel panel-default p50 uth-panel">
                    <table className="table table-hover">
                      <thead>
                        <tr>
                          <th>Code</th>
                          <th>Prenom</th>
                          <th>Nom</th>
                          <th>Email</th>
                          <th>Telephone</th>
                          <th>Action</th>
                        </tr>
                      </thead>
                      <tbody>
                        {clients &&
                          clients.length &&
                          clients.map(client => (
                            <tr key={client.id}>
                              <td>{client.Code} </td>
                              <td>{client.Prenom}</td>
                              <td>{client.Nom}</td>
                              <td>{client.Email}</td>
                              <td>{client.Telephone}</td>
                              <td>
                                <a>Edit</a>|<a>Delete</a>
                              </td>
                            </tr>
                          ))}
                      </tbody>
                    </table>
                  </div>
                </div>
              </CardBody>
            </Card>
          </Col>
        </Row>
      </div>
    );
  }
}

export default ListeClients;

你真的不必申报 self = this . 毫无意义。
您可以直接变异/更改状态属性,而不是使用 this.setState react提供的方法。
你不应该直接改变状态,除非它是 constructor .

ltqd579y

ltqd579y3#

尝试使用完整的url:

axios({
  method: "get",
  url: "http:/[YOUR_SERVER_LOCATION]/app/listeclients/",
  withCredentials: true,
  headers: {
    "Access-Control-Allow-Origin": "*",
    "Content-Type": "application/json",
    Accept: "application/json"
  }
})

我想因为这个你得到的是一个空洞的结果。

相关问题