2

I have below in wcf webconfig

 <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Content-Type, Accept" />
        <add name="Access-Control-Allow-Methods" value="POST,GET,OPTIONS" />
        <add name="Access-Control-Max-Age" value="1728000"/>
      </customHeaders>
    </httpProtocol>

service

 [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "getItem", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        [Description("Fetch item from database.")]

Angular 6 calling

this.http.post('http://localhost:49823/service.svc/getItem?authkey=key',
  {
   json data
}
  ).subscribe(data => {console.log(data) },err => {
    console.log(err);
  });

Interceptor

request = request.clone({
      setHeaders: {
         'Content-Type': 'application/json'
      }
    });

It throws error

Access to XMLHttpRequest at 'http://localhost:49823/service.svc/getitem?authkey=key5' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.

WCF service running on 49823 and angular 6 running on 4200.

Couldn't figure what is causing issue as already enable cors in wcf.

This is working fine in POSTMAN

Md. Parvez Alam
  • 4,326
  • 5
  • 48
  • 108
  • It works fine with Postman because you’re not having Postman send an OPTIONS request. But your browser is sending an OPTIONS request — a CORS preflight OPTIONS request — first before trying the POST request from your code. And the preflight fails so the browser stops right there and never even attempts to send the POST request from your code. And that preflight fails because the response from `http://localhost:49823/service.svc/getItem?authkey=key` to the OPTIONS request isn’t a 200 OK but is instead some 4xx or 5xx error. – sideshowbarker Jan 18 '19 at 11:06
  • So how to resolve this – Md. Parvez Alam Jan 18 '19 at 11:11
  • I called other web api from this angular call, and it is working but call to wcf not working – Md. Parvez Alam Jan 18 '19 at 11:12
  • Did you check if access-control-allow-origin header is getting passed? – Manoj Choudhari Jan 20 '19 at 06:14
  • If you want a 200 ok status code for option method, you could try to write the following code in Application_EndRequest event, if(HttpContext.Current.Request.HttpMethod == "OPTIONS") { HttpContext.Current.Response.StatusCode = 200; } – Ackelry Xu Jan 21 '19 at 09:29

0 Answers0