wzhuwzhu 2018-11-06 08:45 采纳率: 0%
浏览 28656

reactjs无法连接后台:Could not proxy request xxx from localhost:3000 to http://localhost:8080/

package.json写了“proxy”:"http://localhost:8080"
下面是App.js里component StaffList.js代码,cnpm start后console显示异常:
Proxy error: Could not proxy request /api/staffs from localhost:3000 to http://localhost:8080/.
另外页面报错:
Unhandled Rejection (SyntaxError): Unexpected token P in JSON at position 0
出错是这一句: .then(response => response.json())

应该是连接不到后台的Controller?毫无头绪,应该怎么解决?谢谢了!!

 class StaffList extends Component {
  constructor(props) {
    super(props);
    this.state = {staffs: [], isLoading: true};
    this.remove = this.remove.bind(this);
  }

  componentDidMount() {
    this.setState({isLoading: true});

    fetch('api/staffs')
      .then(response => response.json())
      .then(data => this.setState({staffs: data, isLoading: false}));
  }

  async remove(id) {
    await fetch(`/api/staff/${id}`, {
      method: 'DELETE',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      }
    }).then(() => {
      let updatedStaffs = [...this.state.staffs].filter(i => i.id !== id); 
      this.setState({staffs: updatedStaffs});
    });
  }

  render() {
    const {staffs, isLoading} = this.state;

    if (isLoading) {
      return <p>Loading...</p>;
    }

    const staffList = staffs.map(staffManagement => {
      return <tr key={staffManagement.id}>
        <td style={{whiteSpace: 'nowrap'}}>{staffManagement.name}</td>
        <td>
          <ButtonGroup>
            <Button size="sm" color="primary" tag={Link} to={"/staffs/" + staffManagement.id}>Edit</Button>
            <Button size="sm" color="danger" onClick={() => this.remove(staffManagement.id)}>Delete</Button>
          </ButtonGroup>
        </td>
      </tr>
    });

    return (
      <div>
        <AppNavbar/>
        <Container fluid>
          <div className="float-right">
            <Button color="success" tag={Link} to="/staffs/new">Add Staff</Button>
          </div>
          <h3>Staff Management</h3>
          <Table className="mt-4">
            <thead>
            <tr>
              <th width="20%">Name</th>
              <th width="20%">Department</th>
              <th>Position</th>
              <th width="10%">Permission</th>
            </tr>
            </thead>
            <tbody>
            {staffList}
            </tbody>
          </Table>
        </Container>
      </div>
    );
  }
}

export default StaffList;
  • 写回答

4条回答 默认 最新

  • zcbdandan 2021-11-09 14:24
    关注

    反正vue如果是前后台都在同一个机器上启动的话,changeOrigin跨越配置需要配置成false,如果是true,跟你的问题一样。你可以看看是不是这个原因。

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog