Hi, I am new to asp.net core, and react.js component. I wrote a get web api that returns the string like username. How to call this api from react.js component. Please provide a sample code.
Thanks
Hi, I am new to asp.net core, and react.js component. I wrote a get web api that returns the string like username. How to call this api from react.js component. Please provide a sample code.
Thanks
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
saradalalPosted Aug 3, 2021, 4:44 PM
Thanks Piyush for the response. I am still not able to resolve the issue. Please find my code in the zip file. Please make the changes in the code, that will help me in understanding how to call web api from react.js component. This is my first component in react.js.
Thanks
Piyush PansuriyaPosted Aug 3, 2021, 4:40 AM
Hello,
We call fetch method and we will pass the Url of Web API. This is the Url where our Web API is available and I paste it here. We call the Promise method using then, convert the response into JSON objects, and then we assign this result list to our state object employees property using setState method.
Fetch(“https://localhost:44301/api/Employee”)
.then(res => res.json())
.then(
(result) => {
this.setState({
employees: result
});
}
);