Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 使用axios.get(url)和param从数据库获取数据_Reactjs - Fatal编程技术网

Reactjs 使用axios.get(url)和param从数据库获取数据

Reactjs 使用axios.get(url)和param从数据库获取数据,reactjs,Reactjs,我正在尝试使用React中的axios从数据库获取数据。不幸的是,由于某种原因,我使用的url没有返回数据,尽管在浏览器中是这样。url包含一个参数,这很可能是一个问题,因为当我使用没有参数的url时,数据会返回 constructor(props) { super(props); this.state = { userId : this.props.match.params.userId, users:[]

我正在尝试使用React中的axios从数据库获取数据。不幸的是,由于某种原因,我使用的url没有返回数据,尽管在浏览器中是这样。url包含一个参数,这很可能是一个问题,因为当我使用没有参数的url时,数据会返回

 constructor(props) {
        super(props);
        this.state = {
            userId : this.props.match.params.userId,
            users:[]
        }

    }

    componentDidMount() {
        const url = "http://localhost:8080/getUser?userId=1";

        axios.get("http://localhost:8080/getUser", {params: {
            userId: this.state.userId
            }})
            .then(response => response.data)
            .then((data) => {
                this.setState({users: data})
            });

        console.log(this.state.users);
    }
有人知道如何使用RESTAPI和axios正确获取数据库数据吗

axios.get("http://localhost:8080/getUser", {
 params: {
        userId: this.state.userId
        }
 })
 .then(response => response.json()) // You need to parse JSON
 .then((data) => {
    this.setState({users: data})
 });

唯一的区别在于首先。然后,您需要将数据解析为JSON。

您可能应该查看浏览器中的控制台和网络日志,以诊断问题,并登录服务器。您可以看到是否显示了任何错误,并且可以看到确切的请求和响应。