팡이네

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 hostprotocol, and port of your site. Things that might cause this:

서버측에 CORS(Cross-Orgion Resource Sharing) Header를 추가해준다.

그 외 방법은 아래 링크를 참조한다.

https://daveceddia.com/access-control-allow-origin-cors-errors-in-angular/

https://stackoverflow.com/questions/29024313/asp-net-webapi2-enable-cors-not-working-with-aspnet-webapi-cors-5-2-3


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>


MVC4 Quick Tip #3–Removing the XML Formatter from ASP.Net Web API

In Global.asax add the line:

GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();

like so:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);

    BundleTable.Bundles.RegisterTemplateBundles();
    GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
}


'C#' 카테고리의 다른 글

폴더 압축 (SharpZipLib 사용)  (0) 2017.11.28
ClosedXML 사용  (0) 2017.05.17
ASP.NET 엑셀 출력 스타일  (0) 2016.08.31
사진 이미지 관련 클래스  (0) 2016.04.14
오라클 BLOB 등록 및 조회  (0) 2016.04.14