我实际上正在努力用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)