1、配置服务端

在Startup文件中国配置Cors策略:

       IEnumerable<Client> clients= Configuration.GetSection("Clients").Get(typeof(IEnumerable<Client>)) as IEnumerable<Client>;

            List<string> urls = new List<string>();
foreach (var client in clients)
{
urls.AddRange(client.AllowedCorsOrigins);
}
services.AddCors(options =>
{
options.AddPolicy("default",
builder => builder.WithOrigins(urls.ToArray())
.AllowAnyHeader()
.AllowCredentials()
.AllowAnyMethod());
});

启用CORS策略,可以在Startup文件中配置,也可以在具体的ApiController中配置,代码分别如下:

 app.UseCors("default");
    [Authorize]
[EnableCors("default")]
public class NavigationMenuController:Controller
{
private NavigationMenuService navigationMenuService; public NavigationMenuController(NavigationMenuService navigationMenuService)
{
this.navigationMenuService = navigationMenuService;
}
}

2、JQuery请求

        $.ajax({
url: 'http://localhost:5000/api/Private/Values/Identity',
type: 'GET',
dataType: 'json',
crossDomain: true,
xhrFields: {
withCredentials: true
},
success: function (result) {
$('#platformResult').val(JSON.stringify(result));
}
});

红字是关键 ,值得注意的是需要配置 withCredentials,否则请求不会带上Cookie。

3、 axios请求

      axios({
url:
config.authority +
"/api/xxxxxxxxxx?clientId=" +
escape(config.client_id),
method: "GET",
withCredentials: true
}).then(function (result) {
if (result.success == undefined) {
self.menuItems = result || [];
}
});

最新文章

  1. OGNL的使用
  2. adminLTE的自动化菜单
  3. sql操作
  4. 简单模仿javascript confirm方法的例子
  5. 移动平台WEB前端开发技巧汇总
  6. 配置Linux系统网卡连接网络
  7. Winform学习手册(目录)
  8. socket实现局域网通信
  9. javascript 正则介绍
  10. [转]POJ1006: 中国剩余定理的完美演绎
  11. WordCount结对项目
  12. tomcat启动报错 关键字:java.lang.NoClassDefFoundError和 java.lang.ClassNotFoundExceeption
  13. laravel 接口跨域
  14. Python3函数式编程
  15. cxf 相关问题
  16. Centos 7 配置 VNCServer 經驗
  17. 每日笔记---使用@ConfigurationProperties读取yml配置
  18. RelativeLayout相对布局中属性值
  19. 【shiro】2.spring整合shiro,注解控制shiro用户/角色/权限And/OR,没有权限跳转到固定页面
  20. Android 开发 -------- 自己定义View 画 五子棋

热门文章

  1. Android平台使用Ceres Solver
  2. java并发编程(十四)----(JUC原子类)对象的属性修改类型介绍
  3. 牛客多校训练第八场G.Gemstones(栈模拟)
  4. 安装node.js、webpack、vue 和vue-cli 以及安装速度慢/不成功的解决方法
  5. PHP 数组转字符串后仍保留数组格式
  6. Linux 目录递归赋权,解决 Linux权限不够
  7. js作用域链和预编译
  8. Redis的高可用(HA)
  9. 记录一次Jquery中 this 关键字使用出现的问题
  10. MySQL数据库的安装和配置