enabling cross-origin resource sharing on IIS7

后端 未结 10 2081
轻奢々
轻奢々 2020-11-22 15:22

I recently ran into with posting Javascript requests to another domain. By default XHR posting to other domains is not allowed.

Following the instructions from htt

10条回答
  •  死守一世寂寞
    2020-11-22 15:42

    It is likely a case of IIS 7 'handling' the HTTP OPTIONS response instead of your application specifying it. To determine this, in IIS7,

    1. Go to your site's Handler Mappings.

    2. Scroll down to 'OPTIONSVerbHandler'.

    3. Change the 'ProtocolSupportModule' to 'IsapiHandler'

    4. Set the executable: %windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll

    Now, your config entries above should kick in when an HTTP OPTIONS verb is sent.

    Alternatively you can respond to the HTTP OPTIONS verb in your BeginRequest method.

        protected void Application_BeginRequest(object sender,EventArgs e)
        {
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
    
            if(HttpContext.Current.Request.HttpMethod == "OPTIONS")
            {
                //These headers are handling the "pre-flight" OPTIONS call sent by the browser
                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
                HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000" );
                HttpContext.Current.Response.End();
            }
    
        }
    

提交回复
热议问题