小编Fit*_*tch的帖子

如何使用Angular 2在POST上启用ASP.NET MVC 4中的跨源请求

我实际上正在努力用CORS发出POST请求.

我实际上是一个在我的控制器内部的方法上添加正确响应头的类

using System;
using System.Web.Mvc;

public class AllowCrossSiteAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Origin", "http://localhost:4200");
        filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Headers", "*");
        filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Credentials", "true");

        base.OnActionExecuting(filterContext);
    }
}
Run Code Online (Sandbox Code Playgroud)

我就是这样用的

[AllowCrossSite]
public ActionResult GetReportParameters(string IdRapport)
Run Code Online (Sandbox Code Playgroud)

我得到了预期的结果 在此输入图像描述

但问题是当我尝试使用自定义标头发出POST请求以传递此特定内容类型时

'Content-Type': 'application/json; charset=utf-8'
Run Code Online (Sandbox Code Playgroud)

我实际上得到了这个响应头

在此输入图像描述

因此,即使我正确地进入属性类,也就像标题没有做任何事情一样.

这是我在Angular 2服务中的前端

const headers = new Headers({ 'Content-Type': 'application/json; charset=utf-8' });
const options = new RequestOptions({ headers: headers , withCredentials: true });

const mock = {
  name: 'TitreRegroupement1',
  visible: false,
  type: 'System.String',
  value: 'Test'
};

// tslint:disable-next-line:max-line-length
const …
Run Code Online (Sandbox Code Playgroud)

javascript asp.net-mvc cors asp.net-mvc-4 angular

7
推荐指数
1
解决办法
5787
查看次数

标签 统计

angular ×1

asp.net-mvc ×1

asp.net-mvc-4 ×1

cors ×1

javascript ×1