.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 ...
随机推荐
- 如何正确遵守 Python 代码规范
前言 无规矩不成方圆,代码亦是如此,本篇文章将会介绍一些自己做项目时遵守的较为常用的 Python 代码规范. 命名 大小写 模块名写法: module_name 包名写法: package_name ...
- 使用Opencv4和YOLOv4(XTDrone)训练模型遇到问题的记录(二)
使用Opencv4和YOLOv4(XTDrone)训练模型遇到问题的记录(二) Written By PiscesAlpaca(双鱼座羊驼) 目录 使用Opencv4和YOLOv4(XTDrone)训 ...
- 1、ArrayList源码解析
目录 1 概述 2 底层数据结构 3 构造函数 4 自动扩容 5 set() get() remove() 6 Fail-Fast机制 1 概述 ArrayList实现了List接口,是 顺序容器,允 ...
- 项目上的业务《接收一个xml信息包进行解析,xml中包含base64解析为电子文件》
我就直接贴代码了,不太会说,附上注释. ps:需要根据系统字段和xml里面的标签字段进行建表,之后把xml标签的值进行添加.创建表的方法就是拼的sql. // 在线接收接口 @Transactiona ...
- 模块/os/sys/json
目录 内容概要 1.os模块 2.sys模块 3.json模块/实战 内容概要 os模块 sys模块 json模块/实战 1.os模块 # os模块主要是与我们的操作系统打交道 1.创建文件夹(目录) ...
- 重新认识下JVM级别的本地缓存框架Guava Cache(3)——探寻实现细节与核心机制
大家好,又见面了. 本文是笔者作为掘金技术社区签约作者的身份输出的缓存专栏系列内容,将会通过系列专题,讲清楚缓存的方方面面.如果感兴趣,欢迎关注以获取后续更新. 通过<重新认识下JVM级别的本地 ...
- ArcObjects SDK开发 001 ArcObjects SDK 简介
1.什么是ArcObjects SDK 在网上搜索什么是ArcObjects,会搜到如下的定义. 这个定义比较准确,也比较容易理解. 2.什么是ArcEngine 在网上搜索ArcEngine,一般会 ...
- jquery 操作样式
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Apache手动安装教程及报错解决梳理
操作参考教程:https://www.cnblogs.com/haw2106/p/9839655.html 下载地址(例):wget https://dlcdn.apache.org/httpd/ht ...
- 【vue3】element-plus,Checkbox-Group多选框之绑定选中数据不选中问题
今天记录一下在新项目vue3中,使用的element-plus组价库遇到的一个问题!场景如下:有一个表格的column绑定的数组对象,我需要对表格的头部实现动态可配置显示表格列,由于绑定的column ...