1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > asp.net core 2.0 api ajax跨域问题

asp.net core 2.0 api ajax跨域问题

时间:2022-10-12 13:58:23

相关推荐

asp.net core 2.0 api ajax跨域问题

API配置:

services.AddCors(options =>{options.AddPolicy("any", builder =>{builder.WithOrigins("http://localhost") //指定允许来源的主机访问.AllowAnyMethod().AllowAnyHeader().AllowCredentials();//指定处理cookie});});//设置全局筛选器,全局控制器启用coreservices.Configure<MvcOptions>(options =>options.Filters.Add(new CorsAuthorizationFilterFactory("any")));

客户端:

<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title></title><meta charset="utf-8" /><script src="jquery.min.js"></script></head><body><input id="login" value="登录" type="button" /><input id="sava" value="保存" type="button" /><span id="message"></span><script>$("#sava").click(function () {$.ajax({type: 'GET',url: "http://localhost:54821/api/v1/garbage/type",data: { },dataType: "json",//必须有这项的配置,不然cookie无法发送至服务端xhrFields: {withCredentials: true},success: function (result) {alert(result.code);$("#message").html(result);},error: function (xhr,status) {alert("失败");$("#message").html(status);}});})</script></body></html>

此时设置全局启用,控制器中可以使用

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。