Error MVC5: Access-Control-Allow-Origin header is present on the requested resource [closed]

0

I have an application in MVC 5 that uses a component to validate the session but it is generating this error.

  

XMLHttpRequest can not load link . No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' link ' is therefore not allowed access.

It is because you are trying to request a different domain address. How can I enable it so that it does not continue sending me this message?

PS: the message is sent by the browser and it does not allow me to make ajax requests to the controller.

    
asked by agente60 26.07.2016 в 19:53
source

2 answers

1

Option A : If you are only going to give certain pages a chance, in the controller add to your POST , GET or another verb that you use:

HttpContext.Response.AppendHeader("Access-Control-Allow-Origin", "*");

Option B : Modify web.config and provide access to the entire application:

<system.webServer>
    <httpProtocol>
      <customHeaders>
        <clear />
        <add name="Access-Control-Allow-Origin" value="*" />
      </customHeaders>
    </httpProtocol>

And for security reasons, add [ValidateAntiForgeryToken()] to your Actions and in your forms: @Html.AntiForgeryToken() .

    
answered by 26.07.2016 в 20:12
1

You have to enable CORS

Enabling Cross-Origin Requests in ASP.NET Web API 2

if it is webapi service simply in the register

public static void Register(HttpConfiguration config)
{
     config.EnableCors();

     //resto codigo

You could also evaluate how to configure it

How to enable Cross Origin Resource Sharing in MVC

in this case you add this in the web.config

CORS on ASP.NET

    
answered by 26.07.2016 в 20:12