.NET WebAPI 跨域问题(has been blocked by CORS policy:No Access-Control-Allow-Ogigin header is present on the requested resource)
一、什么是跨域
1. 跨域解释
跨域指的是浏览器不能执行其他网站的脚本。它是由浏览器的同源策略造成的,是浏览器施加的安全限制。
同源指的是:域名,协议,端口均相同。
2. 什么情况下会导致跨域
2.1 不同域名(无论主域名还是子域名)
www.dennis.com 访问 www.dong.com
2.2 不同端口号
www.dennis.com 访问 www.dennis.com:8080
2.3 不同协议
http://www.dennis.com 访问 https://www.dennis.com
2.4 本地指向
localhost 访问 127.0.0.1
二、如何解决跨域
1. .NET 版本
1.1 NuGet包安装Microsoft.AspNet.Cors
搜索Microsoft.AspNet.Cors,下载并安装

1.2 配置 WebApiConfig
打开APP_Start文件夹下的 WebApiConfig,最后面加上 config.EnableCors(); 即可
当然这个有重载,支持固定的域名访问和固定的请求方式,可以自行测试
第一个参数是过滤域名的,多个用英文逗号隔开
第二个参数是过滤header的,没测试过,有兴趣自行测试
第三个参数是过滤请求方式的,多个用英文逗号隔开
如:config.EnableCors(new EnableCorsAttribute("*", "*", "*")); 允许所有域名,所有请求方式的方法
如:config.EnableCors(new EnableCorsAttribute("https://www.dennis.com", "*", "get,post")); 允许www.dennis.com域名,所有get和post请求方式的方法
2. .NET Core 版本
2.1 配置Startup
Startup中声明全局私有常量
/// <summary>
/// 跨域配置名称
/// </summary>
private const string DefaultCorsPolicyName = "AllowCross";
在ConfigureServices方法中添加
services.AddCors(options =>
{
options.AddPolicy(DefaultCorsPolicyName, builder =>
{
builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader();
});
});
在Configure方法中的app.UseRouting();//路由后面添加
app.UseCors(DefaultCorsPolicyName);//跨域
.NET WebAPI 跨域问题(has been blocked by CORS policy:No Access-Control-Allow-Ogigin header is present on the requested resource)的更多相关文章
- 前后端分离 导致的 静态页面 加载 <script type="module" > 报CORS 跨域错误,提示 blocked by CORS policy
1.前言 静态页面 加载 <script type="module" > 报CORS 跨域错误,提示Access to script at ftp:///xxx.js ...
- Webapi 跨域 解决解决错误No 'Access-Control-Allow-Origin' header is present on the requested resource 问题
首先是web端(http://localhost:53784) 请求 api(http://localhost:81/api/)时出现错误信息: 查看控制台会发现错误:XMLHttpRequest c ...
- As.net WebAPI CORS, 开启跨源访问,解决错误No 'Access-Control-Allow-Origin' header is present on the requested resource
默认情况下ajax请求是有同源策略,限制了不同域请求的响应. 例子:http://localhost:23160/HtmlPage.html 请求不同源API http://localhost:228 ...
- 解决跨域No 'Access-Control-Allow-Origin' header is present on the requested resource.
用angular发起http.get(),访问后端web API要数据,结果chrome报错:跨域了 Access to XMLHttpRequest at 'http://127.0.0.1:300 ...
- .Net Core 处理跨域问题Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource
网页请求报错: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Or ...
- js跨域访问,No 'Access-Control-Allow-Origin' header is present on the requested resource
js跨域访问提示错误:XMLHttpRequest cannot load http://...... No 'Access-Control-Allow-Origin' header is prese ...
- (转)AJax跨域:No 'Access-Control-Allow-Origin' header is present on the requested resource
在本地用ajax跨域访问请求时报错: No 'Access-Control-Allow-Origin' header is present on the requested resource. Ori ...
- ajax跨域(No 'Access-Control-Allow-Origin' header is present on the requested resource)
问题 在某域名下使用Ajax向另一个域名下的页面请求数据,会遇到跨域问题.另一个域名必须在response中添加 Access-Control-Allow-Origin 的header,才能让前者成功 ...
- java、ajax 跨域请求解决方案('Access-Control-Allow-Origin' header is present on the requested resource. Origin '请求源' is therefore not allowed access.)
1.情景展示 ajax调取java服务器请求报错 报错信息如下: 'Access-Control-Allow-Origin' header is present on the requested ...
- 跨域问题解决----NO 'Access-Control-Allow-Origin' header is present on the requested resource.Origin'http://localhost:11000' is therfore not allowed access'
NO 'Access-Control-Allow-Origin' header is present on the requested resource.Origin'http://localhost ...
随机推荐
- 【Java并发006】使用层面:Lock锁机制全解析
一.前言 二.synchronized局限性 + Lock锁机制的引入 2.1 synchronized局限性 第一,使用synchronized,其他线程只能等待直到持有锁的线程执行完释放锁(syn ...
- element-ui 对话框dialog里使用echarts,报错'dom没有获取到'?
给el-dialog添加@open="open()" 在刚进入页面的时候对话框是关闭的,echarts不进行获取dom,当点击对话框出来的时候,有个opened事件,在这个事件里边 ...
- PyQt5程序打包出错Failed to execute script
出现这种报错一般有两种可能: 1. 该被引入的资源你没有放到 exe 的相对路径 这种错误一般是你直接引入图片或者图标, 而没有放到 打包后的exe的相对路径 2. 加参数 假设 main.py 为程 ...
- JUC源码学习笔记6——ReentrantReadWriteLock
系列文章目录和关于我 阅读此文需要有AQS独占和AQS共享的源码功底,推荐阅读: 1.JUC源码学习笔记1--AQS独占模式和ReentrantLock 2.JUC源码学习笔记2--AQS共享和Sem ...
- PL/SQL Developer使用中文条件查询时无数据的解决方法
1.在PL/SQL Developer中执行sql命令:select userenv('language') from dual; 显示结果为:AMERICAN_AMERICA.ZHS16GBK: 2 ...
- a标签跳新链接,如果链接为空则不跳转
a标签跳新链接,如果链接为空则不跳转 <el-carousel-item v-for="item in slideList" :key="item.id" ...
- LeetCode HOT 100:子集(简单易懂的回溯)
题目:78. 子集 题目描述: 给你一个整数数组,数组中元素互不相同.返回数组中所有可能的子集,且子集不能重复! 什么是子集?举个例子:原数组[1, 2, 3],[].[1].[1, 2].[1, 3 ...
- 万万没想到,go的数据库操作,也能像php一样溜了
Hi,各位go的小伙伴. 很多人都是从php转过来的吧,不知道你们有没有发现,go界的orm并没有像php的orm一样好用.这篇文章里,我们认真的讨论下这个问题,并且会在后面提出解决方案. php的方 ...
- Proxmark3 Easy 如何流畅的在Linux中操作?
前言 Proxmark3 Easy可谓是国内能买到的比较便宜的RFID卡防设备了,曾在部分hv中出现. Proxmark3 Easy是国内简化的一种设备,利用的是较早的Proxmark3的开源,基础的 ...
- [深度探索C++对象模型]memcpy和memset注意事项
不管使用memcpy()还是memset(),都只有在"classes不含任何由编译器产生的内部members"时才能运行.如果这个类声明一个或者一个以上的virtual func ...