问题描述
enter image description here我是React的新手,如果能得到帮助,那将非常好。我创建了一个Api getUserDetails并在Postman上对其进行了测试,结果显示效果理想,但现在我正尝试从该本地获取数据Api在我的ReactJs应用程序中,但无法获取任何数据。我该怎么做才能从我的ReactJs应用程序中的localHost Api中获取数据。这是我的React代码:
class App extends Component {
constructor(){
super();
this.state ={
user:[],}
}
componentDidMount(){
fetch("http://localhost:8080/getUserDetails?name=Anisha")
.then(response=>response.text)
.then(data=>{
this.setState({user:data})
})
}
getUser(){
const users=this.state.user.map((u)=>
<div className="userBox">
<div>{u.name}</div>
</div>
)
return users;
}
render(){
return (
<div>{this.getUser()}</div>
)
}
}
,后端代码如下:
@RestController
public class UserController {
@GetMapping("/getUserDetails")
public String getUserDetails(@RequestParam String name) {
return "Requesting Details of User" + name ;
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)