如何将reactJS前端与PHP连接?

问题描述 投票:-1回答:1

我正在使用ReactJS开发前端。

如何通过PHP(普通PHP或Yii1)提供此应用程序?

php reactjs yii
1个回答
0
投票

我想你想要通过HTTP协议来创建两者之间的通信。

在ReactJSX中,您希望进行从后端提供EndPoint / REST的提取/获取调用。

// at the beginning of your file
import axios from 'axios';

// in your component constructor
state = {
  yourStateWithTheRecievedData: "",
}

// as a component method
componentDidMount = () => {
  axios
    .get('http://localhost:8080/YourBackendAPI')
    .then(response => {
      this.setState({
        yourStateWithTheRecievedData: response.data.yourStateWithTheRecievedData
      })
      console.log(response.data.yourStateWithTheRecievedData)
    })
}

您现在应该拥有一个包含您收到的数据的状态。如果您很难做到这一点,我建议您首先将其应用于公共API。您可以找到公共API的here列表

© www.soinside.com 2019 - 2024. All rights reserved.