Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wcf webHttpBinding Response for preflight has invalid HTTP status code 400

I followed Ido Flatow's post to create a cross-origin request and created the following:

On my service interface:

[OperationContract]
[WebInvoke(Method = "*", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
bool GetUserAuthentication(string userLoginName, string password);

On app.config:

<behaviors>
  <endpointBehaviors>
    <behavior name="webSupport">
      <webHttp />
      <CorsSupport />
    </behavior>
  </endpointBehaviors>
</behaviors>

<extensions>
  <behaviorExtensions>
    <add name="CorsSupport" type="WebHttpCors.CorsSupportBehaviorElement, WebHttpCors, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
  </behaviorExtensions>
</extensions>

Now I'm sending a simple post request using angular $http service and it's working on IE & Edge (also on Postman) but fails to work on Chrome & Firefox with the following error:

XMLHttpRequest cannot load http://localhost:5280/MetaDataService/GetUserAuthentication. Response for preflight has invalid HTTP status code 400

Chrome error printscreen:

Error

like image 926
Dor Cohen Avatar asked Aug 05 '26 21:08

Dor Cohen


1 Answers

Chrome is preflighting the request to look for CORS headers. If the request is acceptable, it will then send the real request.

My problem was that the OPTIONS request which sent to my WCF server was rejected I defined Method="*" for my method, causing the options request to be directed to my method and was expecting to get also parameters.

The solution for this based on How to handle Ajax JQUERY POST request with WCF self-host question was to add new OperationContract for dealing with OPTIONS requests:

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

public void GetOptions()
{
}

this method gets all options request and allowing the real POST method being directed to my service method.

like image 145
Dor Cohen Avatar answered Aug 08 '26 10:08

Dor Cohen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!