PDF被Cors请求ASP.NET阻止

时间:2018-10-26 21:22:30

标签: javascript asp.net asp.net-mvc asp.net-web-api

我正在尝试通过JS提取请求pdf文件,但是该请求被HTTP 405阻止。我使用ISS模块管理该请求,该模块将Access-Control-Allow-Origin添加到所有地址。我该如何解决?

Js脚本

fetch(link, {
    mode: 'cors',
    crossdomain: true,
    method: 'GET',
    headers: {
    'Content-Type': "application/pdf",
    'Accept': "application/pdf",
    'Authorization': "Bearer " + token,
    },
}).then(res => {console.log(res);});

是Mod

public class PdfCordMod : IHttpModule
{
    public PdfCordMod()
    {
    }

    public String ModuleName
    {
        get { return "PdfCordMod"; }
    }

    // In the Init function, register for HttpApplication 
    // events by adding your handlers.
    public void Init(HttpApplication application)
    {
        application.BeginRequest +=
            (new EventHandler(this.Application_BeginRequest));
    }

    private void Application_BeginRequest(Object source,
         EventArgs e)
    {
        // Create HttpApplication and HttpContext objects to access
        // request and response properties.
        HttpApplication application = (HttpApplication)source;
        HttpContext context = application.Context;
        string filePath = context.Request.FilePath;
        string fileExtension = VirtualPathUtility.GetExtension(filePath);
        if (fileExtension.Equals(".pdf") )
        {
            context.Response.AddHeader("Access-Control-Allow-Origin", "*");
            context.Response.AddHeader("Access-Control-Allow-Methods", " GET, POST, PATCH, PUT, DELETE, OPTIONS");
            context.Response.AddHeader("Access-Control-Allow-Headers", "Origin, Content-Type, X-Auth-Token");
        }
    }

    public void Dispose() { }
}

0 个答案:

没有答案