Use jQuery.getJson to get Web API

This question already has an answer here:

  • What is JSONP all about? [duplicate] 7 answers

  • 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(new EnableCorsAttribute("*","*","*"));
    

    WebApiConfig:

    public static class WebApiConfig
    {
        public static void 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 }
            );
        }
    
    链接地址: http://www.djcxy.com/p/8348.html

    上一篇: 拒绝在jsonp请求上执行脚本

    下一篇: 使用jQuery.getJson获取Web API