客户端js无需任何专门设置,使用通常的ajax调用即可:

$.ajax({
    url: '跨域URL',
    type: 'get',
    dataType: 'json',
    success: function (data) {
        $('#banner_right').html(data);
    }
});

服务端需要在WebApiConfig.Register()中添加如下的代码

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        var cors = new EnableCorsAttribute("http://www.cnblogs.com,http://news.cnblogs.com", "*", "*");
        config.EnableCors(cors);            
    }
}

注1:"http://www.cnblogs.com,http://news.cnblogs.com"表示允许这2个域名可以跨域调用这个Web API。

注2:需要nuget安装Microsoft.AspNet.Cors

相关文章:

  • 2021-12-26
  • 2022-12-23
  • 2022-12-23
  • 2021-12-08
  • 2021-07-09
  • 2021-07-28
  • 2022-12-23
猜你喜欢
  • 2022-01-18
  • 2022-01-07
  • 2021-09-25
  • 2021-10-04
  • 2021-06-18
  • 2021-08-17
相关资源
相似解决方案