AngularJS performs an OPTIONS HTTP request for a cross-origin resource

后端 未结 14 1641
生来不讨喜
生来不讨喜 2020-11-22 03:04

I\'m trying to setup AngularJS to communicate with a cross-origin resource where the asset host which delivers my template files is on a different domain and therefore the X

14条回答
  •  一整个雨季
    2020-11-22 03:31

    For an IIS MVC 5 / Angular CLI ( Yes, I am well aware your problem is with Angular JS ) project with API I did the following:

    web.config under node

        
          
          
        
        
          
            
            
            
            
          
        
    

    global.asax.cs

    protected void Application_BeginRequest() {
      if (Request.Headers.AllKeys.Contains("Origin", StringComparer.OrdinalIgnoreCase) && Request.HttpMethod == "OPTIONS") {
        Response.Flush();
        Response.End();
      }
    }
    

    That should fix your issues for both MVC and WebAPI without having to do all the other run around. I then created an HttpInterceptor in the Angular CLI project that automatically added in the the relevant header information. Hope this helps someone out in a similar situation.

提交回复
热议问题