1

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
  • CORS issue cannot solve at Front-end. It depends on server settings. – Humanoid Mk.12 Jul 13 '23 at 08:18
  • @HumanoidMk.12 Is there anything else that could be triggering that error? because I've already added CORS policy in my program.cs as shown above. – Mohamed Yousef Khairlden Jul 13 '23 at 08:26
  • There are lots of things can cause the CORS. AWS or something's config, firewall, even resource path. – Humanoid Mk.12 Jul 13 '23 at 08:33
  • preflight goes well? you can check it on the developer mode. – Humanoid Mk.12 Jul 13 '23 at 08:34
  • How did you send the request? Have you tried to [set to development environment in web.config](https://stackoverflow.com/a/37455594/18789859) to see details? Can you provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example)? – Chen Jul 14 '23 at 03:06
  • @Chen I've added an example to the axios request i'm sending to the backend, which works completely fine locally. – Mohamed Yousef Khairlden Jul 16 '23 at 09:42
  • There is nothing wrong with your request. What confuses me is that you defined port 5000 in the .env file, but requested port 5030. If this is just a typo, try using `https://` instead of `http://`. Refer to [this link](https://stackoverflow.com/questions/62861287/enable-cors-is-not-working-after-publishing-into-iis). – Chen Jul 17 '23 at 07:23

0 Answers0