기타
Web API CORS 관련 문제 해결(개발 시)
네오팡
2017. 12. 22. 10:16
Angular에서 localhost Web API를 호출할 때 아래와 같은 오류가 발생할 경우
http failure response for (unknown url) 0 unknown error
You’ve run afoul of the Same Origin Policy – it says that every AJAX request must match the exact host, protocol, and port of your site. Things that might cause this:
- Hitting a server from a locally-served file (a request from
file:///YourApp/index.html
tohttp://api.awesome.com
) - Hitting an external API (a request from
http://yourapp.com
tohttp://api.awesome.com
). - Hitting an internal API (a request from
http://yourapp.com
tohttp://api.yourapp.com
). - Hitting a different port on the same host (webapp is on
http://localhost:3000
, API ishttp://localhost:4000
) - Requesting over
http
fromhttps
or vice-versa (requestinghttps://yourapp.com
fromhttp://yourapp.com
)
서버측에 CORS(Cross-Orgion Resource Sharing) Header를 추가해준다.
그 외 방법은 아래 링크를 참조한다.
https://daveceddia.com/access-control-allow-origin-cors-errors-in-angular/
I just added custom headers to the Web.config and it worked like a charm.
On configuration - system.webServer:
<httpProtocol> <customHeaders> <add name="Access-Control-Allow-Origin" value="*" /> <add name="Access-Control-Allow-Headers" value="Content-Type" /> </customHeaders> </httpProtocol>