wzhuwzhu 2018-11-07 08:14 采纳率: 0%
浏览 2739

Reactjs fetch()报错net::ERR_EMPTY_RESPONSE

以下是Controller和Test.js代码
在Test.js里试了console.log()和setState()都获取不到数据。
写了text.json测试是可以用fetch()获取到里面的数据的,不知道为什么Controller的数据获取不到。
Controller用main方法测试过,api/staffs指向的staffs()方法可以获取到数据库的数据。
刷新页面,过半分钟console才报错:
Test.js:14 GET http://localhost:8080/api/staffs net::ERR_EMPTY_RESPONSE
错误出在fetch()命令。

新手求解,谢谢了!!

@RestController
@RequestMapping("/api")
public class StaffManagementController {
    @Autowired 
    private StaffManagementFunctionImpl function = new StaffManagementFunctionImpl();
    public StaffManagementController(StaffManagementFunctionImpl function) {
        this.function = function;
    }   
    @GetMapping("/staffs")
    List<StaffManagement> staffs() {
        return function.findAllStaff();
    }
}   
 class Test extends Component {
  constructor(props) {
    super(props);
    this.state = {staffs: []};
  }

  componentDidMount() {
    fetch('api/staffs')
      .then(response => response.json())
      .then((result) => {console.log(result)})
      //.then(item => this.setState({staffs: item.staffManagement}))
      .catch(e=>{console.log("error")})
  }

  render() {
    const staffs = this.state.staffs;
    return (
        <div>
            <ul>
              {staffs.map((item) => {
                  return <li key={item.id}>{item.name}</li>
                  })}
            </ul>
        </div>
    )
  }
}

export default Test;
  • 写回答

2条回答 默认 最新

  • dabocaiqq 2018-11-07 12:01
    关注
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料