I've an asp.net core with react app, It was and still running fine on my pc. But when i published the project to my windows server IIS I started getting errors like this:
{
"message": "Network Error",
"name": "AxiosError",
"stack": "AxiosError: Network Error\n at l.onerror (http://IpAddress/static/js/main.f00ac167.js:2:700322)",
"config": {
"transitional": {
"silentJSONParsing": true,
"forcedJSONParsing": true,
"clarifyTimeoutError": false
},
"adapter": [
"xhr",
"http"
],
"transformRequest": [
null
],
"transformResponse": [
null
],
"timeout": 0,
"xsrfCookieName": "XSRF-TOKEN",
"xsrfHeaderName": "X-XSRF-TOKEN",
"maxContentLength": -1,
"maxBodyLength": -1,
"env": {},
"headers": {
"Accept": "application/json, text/plain, */*"
},
"method": "get",
"url": "http://IpAddress:5030/api/user/null"
},
"code": "ERR_NETWORK",
"status": null
}
Access to XMLHttpRequest at 'http://IpAddress:5030/user/noti/null' from origin 'http://IpAddress' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
I have 2 websites on IIS, one is located at the entire build folder which is the backend with :5030 port. and the other is located at the wwwroot folder only and this one has :80 port. It's not a CORS problem because I've this in my program.cs:
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAll", policy =>
{
policy.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
});
var app = builder.Build();
// Configure the HTTP request pipeline.
app.UseStaticFiles();
app.UseRouting();
app.UseCors("AllowAll");
I've tried adding this to the web.config:
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
But with no luck. any idea what can be causing this?
-Edit
This is an example to the backend requests i make:
const apiUrl = process.env.REACT_APP_API_URL;
useEffect(() => {
const fetchData = async () => {
try {
const response = await axios.get(`${apiUrl}/api/admin`);
const { data } = response.data;
} catch (error) {
}
};
fetchData();
}, []);
const handletReq = async (formData) => {
const taskReq = {
reply: formData.result,
};
axios.post(`${apiUrl}/api/admin/taskreq`, taskReq)
.then((response) => {
// Handle the response
})
.catch((error) => {
// Handle the error
});
};
and this is the .env file:
REACT_APP_API_URL=http://52.29.75.216:5000