使用jQuery.getJson获取Web API

这个问题在这里已经有了答案:

  • 什么是JSONP? [复制] 7个答案

  • 您需要在您的WebAPI中启用CORS。 首先,安装此Nuget - https://www.nuget.org/packages/Microsoft.AspNet.WebApi.Cors,然后将此行添加到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/8347.html

    上一篇: Use jQuery.getJson to get Web API

    下一篇: Angular 2 JSONP, injected script did not invoke callback