Skip to content Skip to sidebar Skip to footer

Use Jquery.getjson To Get Web Api

I'm a beginner of ASP.NET Web API. Fail to use jQuery.getJson() to get ASP.NET Web API this failed: //in 'file:///C:/Users/lil/Desktop/index.html' var url = 'http://localhost:561

Solution 1:

You need enable CORS in you WebAPI. Firstly, install this Nuget - https://www.nuget.org/packages/Microsoft.AspNet.WebApi.Cors and then add this line to WebApiConfig:

config.EnableCors(newEnableCorsAttribute("*","*","*"));

WebApiConfig:

publicstaticclassWebApiConfig{
    publicstaticvoid Register(HttpConfiguration config)
    {

        config.EnableCors(new EnableCorsAttribute("*","*","*"));

        // Web API configuration and services// Configure Web API to use only bearer token authentication.
        config.SuppressDefaultHostAuthentication();
        config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }

Post a Comment for "Use Jquery.getjson To Get Web Api"