0

I am working on a project for a company, everything is working fine when I use the server computer to test it, but when I try using the the employees computer to login, its not connecting.

Here's the am getting from chrome developer tools::: Access to XMLHttpRequest at 'http://localhost/smarthrapi/api/auth' from origin 'http://hrm' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

OPTIONS http://localhost/smarthrapi/api/auth 500 (Internal Server Error)

services.AddCors(o => o.AddPolicy("SmartHRCorsPolicy", builder =>
        {
            builder.AllowAnyOrigin()
                   .AllowAnyMethod()
                   .AllowAnyHeader();
        }));

Cors is already enabled and this is the usage here

[EnableCors("SmartHRCorsPolicy")]
public class AuthController : Controller
{ mycode here
  1. If I use http://localhost/hrmanager/HR/login to log in on the server, its working fine, but when I try http://servername/hrmanager/HR/login on the employees computer, its not working. its as if its not seeing my Code or can't access the database.
  2. All the views are working fine, like when I try http://servername/hrmanager/HR/anotherpage the page is showing but nothing is working.

Is it that they're some restrictions on the server or is the problem from my code, I don't really know please help me. I wrote the code with MVC, I have APIs I called using ajax and am using sqlserver. Please the app need to be Live tomorrow, which means I need to fix it today. Thanks

1 Answers1

1

Looking at the error message you're receiving, it's definitely a CORS issue. To further investigate it, Are you sure that the API is hosted or available on each client's http://localhost/ as mentioned in the error message? If yes, then setting up CORS may help.

If no, are they hosted on http://servername/ ? Then please check the API URLs, they should point to http://servername/ and not point to localhost in that case. Possible areas to look at are View (.cshtml/.vbhtml) files and Javascript code if any.

  • Please I don't understand what you mean by **If no, are they hosted on http://servername/ ? Then please check the API URLs, they should point to http://servername/ and not point to localhost in that case. Possible areas to look at are View (.cshtml/.vbhtml) files and Javascript code if any.** – Emmanuel Ikechukwu Feb 28 '19 at 12:00
  • All the API url are pointing to the servername and not localhost – Emmanuel Ikechukwu Feb 28 '19 at 12:06
  • 1
    Sorry that the sentence confused you for a moment. Can you try to access the API using Postman or simply using a browser from the employee's machine? The error suggests that the application is trying to access the API at http://localhost/ and not at http://servername/. You can check that in the network tab of the Developer tools in Chrome or Firefox – Rushikesh Kshirsagar Feb 28 '19 at 12:12
  • I just changed the route to server name now, it can access the api from the employees machine now – Emmanuel Ikechukwu Feb 28 '19 at 12:25
  • should I also do same for the views? – Emmanuel Ikechukwu Feb 28 '19 at 12:26
  • Awesome! Keep up the great work. Please don't forget to up-vote the answer if it helped you solve the issue. Thanks – Rushikesh Kshirsagar Feb 28 '19 at 12:30
  • everything is working fine now, thanks for your help, I was using a different route didn't even notice it.... Thanks – Emmanuel Ikechukwu Feb 28 '19 at 14:18