weixin_33705053 2017-05-11 06:00 采纳率: 0%
浏览 24

跨域Cors问题

We have a very badly written legacy WebSite application that we are trying to slowly convert to MVC.

As part of this we will be creating a set of MVC controllers that we would like to be able to call from the legacy website as a stopgap measure.

So far I have the following in the website aspx page as an event on the click of a button;

function CallControllerMethod() {
    $.ajax({
        type: 'GET',
        dataType: "text",
        url: "http://localhost:49443/Home/TestAjaxMethod",
        success: function (response) {
            alert(response);
        },
        error: function (e) {
            console.log(e);
        }
    });
}

And this calls a controller method in the MVC project;

[EnableCors("*", "*", "*")]
public class HomeController : Controller
{
    [HttpGet]
    [EnableCors("*","*","*")]
    public int TestAjaxMethod()
    {
        return 10;
    }
}

In the WebApiConfig in the MVC app I have this;

    var cors = new EnableCorsAttribute("*", "*", "*");
    configuration.EnableCors(cors);

So when i call the controller method from the website, my breakpoint in the mvc controller is hit. However, when I return the value of 10, on the website ajax call I get the following error;

XMLHttpRequest cannot load http://localhost:49443/Home/TestAjaxMethod. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:49252' is therefore not allowed access.

What's confusing is that the Ajax call makes it to the controllers method but I can't seem to return the value from it.

Update

Even if I remove all references to Cors from the WebApiConfig and from the controller, the MVC method is still reached and I get the same error.

  • 写回答

1条回答 默认 最新

  • weixin_33730836 2017-05-11 08:31
    关注

    As noted in the comments, the EnableCors attribute only applies to WebAPI controllers. If you want to add a CORS header to a regular MVC method, you could do it manually.

    For example here is some code I've used in the past:

    var origin = Request.Headers["Origin"];
    
    if (origin != null)
    {
        Response.AddHeader("Access-Control-Allow-Origin", origin);
    }
    

    Alternatively, you can create your own attribute:

    public class AddCorsHeader : ActionFilterAttribute
    {
        public override void OnActionExecuted(HttpActionExecutedContext context)
        {
            IEnumerable<string> origin;
            if (context.Request.Headers.TryGetValues("Origin", out origin))
            {
                context.Response.Headers.Add("Access-Control-Allow-Origin", origin);
            }
        }
    }
    

    and then add [AddCorsHeader] to the relevant methods.

    评论

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试