-1

I am doing AngularJS2 with Restful web API , and getting some console errors while calling HTTP post. Errors are listed below,

enter image description here Hope you help

Controller code snippet

namespace SoomWebApi.Controllers
{
    [EnableCors(origins: "http://localhost:64336", headers: "*", methods: "*")]
    public class UserController : ApiController
    {

        public HttpResponseMessage UserLogin([FromBody] LoginRequestData objValidate)
        {
            UserModelManager _UserModelManagerr = new Models.UserModelManager();

            return Request.CreateResponse(HttpStatusCode.OK, _UserModelManagerr.Login(objValidate), GetJSONFormatter());

        } 
    }
}

Webapiconfig code snippet

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services
        config.EnableCors();

        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}
slavoo
  • 5,798
  • 64
  • 37
  • 39
NIMISHA
  • 3
  • 3
  • Refer [Old Post](http://stackoverflow.com/questions/35721206/how-to-enable-production-mode-in-angular-2) that will help you solve first error.. – MarmiK Jan 02 '17 at 09:53

3 Answers3

0

It look like a CORS problem .. try to put in your Startup.cs file in WebAPI something like:

 using Microsoft.Owin;
using Owin;

 public void Configuration(IAppBuilder app)
        {
            app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
            ConfigureAuth(app);

        }

If you want to limit to some specific host ..try something like:

  var corspolicy = new corspolicy
            {
                allowanymethod = true,
                allowanyheader = true,
                supportscredentials = true,
                origins = { "http://www.example.com", "http://localhost:38091", "http://localhost:39372" }

            };

            app.usecors(new corsoptions
            {
                policyprovider = new corspolicyprovider
                {
                    policyresolver = context => task.fromresult(corspolicy)
                }
            });
federico scamuzzi
  • 3,708
  • 1
  • 17
  • 24
0

Your server should support CORS to allow access from different domains. Update your server configuration to allow only predefined set of domains to allow access.

Access-Control-Allow-Origin: "domains"

If you have this problem on debug only and do not plan use CORS after deploy - mostly local web server starts your Angular application on localhost:some-port and server deployed on localhost also - use IE for debugging - it works good even when your angular middleware and backend works on different ports.

VadimB
  • 5,533
  • 2
  • 34
  • 48
0

If the code in production will be on the same domain as REST API, you can install chrome extension to avoid CORS error:

Chrome CORS extension

Igor Janković
  • 5,494
  • 6
  • 32
  • 46