This may be a duplicate question.
I'm using react for frontend and asp.net core Web API for backend. I tried to fetch data using react query but got an error like this.
Access to XMLHttpRequest at 'https://localhost:7036/api/{Controller Name}/{Action}' from origin 'https://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Now, I have seen a similar answer for this error, in this post Access to XMLHttpRequest at '...' from origin 'localhost:3000' has been blocked by CORS policy But, I would like to know is there any standard or superior way to solve this using react query ?
Controller and Action
[Route("api/[controller]")]
[ApiController]
public class GeneralController : ControllerBase {
public JsonResult GetMatureVerifyData() // Action Method
{
using(var model = new MatureVerifiedDataDTO()){
return new JsonResult(model);
}
}
}
React Query Code
import axios from "axios";
import { useQuery } from "react-query";
export const APIcall = () => {
const { isLoading, error, } = useQuery("verify", () =>
axios.get(
"https://localhost:7036/api/General/GetMatureVerifyData"
).then((res) => console.log(res.data))
);
if (isLoading) {
console.log("Loading...");
};
if (error) {
console.log(`An error has occurred: ${error.message}`);
}
}