locked
Error - Access to XMLHttpRequest at 'http://localhost:40991/api/Employees/' from origin 'http://localhost:24495' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header RRS feed

  • Question

  • User1979860870 posted

    Hi

      I am getting below error

    Access to XMLHttpRequest at 'http://localhost:40991/api/Employees/' from origin 'http://localhost:24495' 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.

    In WebApiConfig i have defined - config.Routes.MapHttpRoute(
                    name: "DefaultApi",
                    //routeTemplate: "api/{controller}/{action}/{id}",
                    routeTemplate: "api/{controller}/{id}",
                    defaults: new { id = RouteParameter.Optional }
                );
                config.EnableCors(new EnableCorsAttribute("*", "*", "GET,PUT,POST,DELETE"));

    Thanks

    Saturday, February 27, 2021 6:31 AM

All replies

  • User475983607 posted

    I cannot reproduce this problem using you code and following the official documentation.

    https://docs.microsoft.com/en-us/aspnet/web-api/overview/security/enabling-cross-origin-requests-in-web-api

    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services
        config.EnableCors(new EnableCorsAttribute("*", "*", "GET,PUT,POST,DELETE"));
    
        config.MapHttpAttributeRoutes();
    
        // Web API routes
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
        public class ValuesViewModel
        {
            public string value { get; set; }
        }
        public class ValuesController : ApiController
        {
            // GET api/values
            public IEnumerable<string> Get()
            {
                return new string[] { "value1", "value2" };
            }
    
            // GET api/values/5
            public int Get(int id)
            {
                return id;
            }
    
            // POST api/values
            public ValuesViewModel Post([FromBody] ValuesViewModel value)
            {
                return value;
            }
        }
    @{
        ViewBag.Title = "Index";
    }
    
    <h1>Index</h1>
    
    <input id="Button1" type="button" value="button" />
    
    
    @section scripts{
    <script>
        $('#Button1').click(function () {
                   
            $.ajax({
                type: 'post',
                url: 'https://localhost:44310/api/values',
                contentType: "application/json",
                content: 'json',
                data: JSON.stringify({'value': 'Hello World'}),
                success: function (result) { 
                    console.log(result);
                }
            });
        });
    </script>
    }

    There must be other issues with your code that you have not shared.  I recommend performing basic troubleshooting and debugging.  If you community debugging support then we'll need code that reproduces this issue.

    Saturday, February 27, 2021 12:10 PM
  • User-474980206 posted

    Preflight uses option verb

    Saturday, February 27, 2021 4:13 PM
  • User1979860870 posted

    Hi Bruce

       What does this mean

    Thanks

    Sunday, February 28, 2021 6:25 AM
  • User1527971034 posted

    http s://ufa88.com/ufpq8820884

    Sunday, February 28, 2021 8:30 AM