Thursday, February 18, 2016

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http:// localhost' is therefore not allowed access.

AngularJS which calls an API- WebMethod for json data. When cross domain origin problem got this error:
Error: 
XMLHttpRequest cannot load api
 No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '.....' is therefore not allowed access.

Solution: 

Add to web.config:


    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Content-Type, accessToken, signedInUserId, x-requested-with" />
      </customHeaders>
    </httpProtocol>