0

My services on two server nodes when i consume it sometimes it works well and sometimes get error

XMLHttpRequest cannot load.. Response for preflight has invalid HTTP status code 404

I followed answers on stackoverflow and wrote this code but it didn't solve the problem

 protected void Application_BeginRequest()
    {
        if (Request.HttpMethod == "OPTIONS")
        {
            Response.Flush();
        }
    }

Web.config

  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <remove name="X-Powered-By" />
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Accept, Content-Type, Origin" />
        <add name="Access-Control-Allow-Methods" value="GET, PUT, POST, DELETE, OPTIONS" />
      </customHeaders>
    </httpProtocol>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="1073741824" />
      </requestFiltering>
    </security>
    <modules>
      <remove name="FormsAuthentication" />
    </modules>
    <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <remove name="TRACEVerbHandler" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer>
Alex
  • 195
  • 3
  • 3
  • 12
  • Possible duplicate of [AngularJS POST Fails: Response for preflight has invalid HTTP status code 404](https://stackoverflow.com/questions/33660712/angularjs-post-fails-response-for-preflight-has-invalid-http-status-code-404) – Nikhil Vartak Jul 17 '17 at 22:21

1 Answers1

0

You probably need a operation contract with this decoration [WebInvoke(Method = "OPTIONS", UriTemplate = "*")], for example:

    [OperationContract]
    [WebInvoke(Method = "OPTIONS", UriTemplate = "*")]
    void GetOptions();

and the operation implementation would be:

    public void GetOptions()
    {
        WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*");
        WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
        WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Headers", "Content-Type");
    }
Bob Dust
  • 2,370
  • 1
  • 17
  • 13
  • Thank you for your help and effort. Can you please tell where i place your code in my services project? – Alex Jul 18 '17 at 13:10
  • The operation contract should be placed in the same service contract to which the call from client-side encounters the error and the operation implementation should be placed in respective service class. – Bob Dust Jul 18 '17 at 14:15