Reputation: 43
I have deployed my react app and .net core api on server. .net core api call works fine when I checked from browser. But when I invoke it from react app it gives 204 no-content. Everything works fine in local.
I have enabled CORS like this
options.AddPolicy(MyAllowSpecificOrigins,
builder =>
{
builder.WithOrigins("http://serverip:5000",
"http://serverip:3000"
}
api call---
[HttpGet]
[Route("path/{id}")]
public async Task<ActionResult<List<myData>>> GetByType(byte id)
{
if (IsUserAuthenticated())
return await _myRepo.GetMyData(id);
else
return null;
}
react call-----
axios.get('http://ip:5000/api/value1/value2/' + param1, {
withCredentials: true })
Appreciate any help !
Upvotes: 0
Views: 1637
Reputation: 43
At the end,it was a silly mistake from my end. I had hardcoded localhost url somewhere in the flow and that was causing CORS issue when accessing from server. Appreciate all the help !
Upvotes: 1
Reputation: 26430
Try to change the code to
[HttpGet]
[Route("path/{id}")]
public async Task<ActionResult<List<myData>>> GetByType(byte id)
{
if (IsUserAuthenticated())
return await _myRepo.GetMyData(id);
else {
HttpContext.Response.StatusCode = 401;
return null;
}
}
This will make sure that the NoContent 204 result is not just the null part of the code.
If you get the unauthorized response, then you need to figure out your security.
By the way, you could use a filter for authorization or an attribute instead of writing it on every action.
Upvotes: 1
Reputation: 3991
Your api route is
[Route("path/{id}")]
you should change
axios.get('http://ip:5000/api/value1/value2/' + param1, { withCredentials: true })
to
axios.get('http://ip:5000/api/path/' + param1, { withCredentials: true })
Upvotes: 1