一、什么是跨域

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)的更多相关文章

  1. 前后端分离 导致的 静态页面 加载 <script type="module" > 报CORS 跨域错误,提示 blocked by CORS policy

    1.前言 静态页面 加载 <script type="module" > 报CORS 跨域错误,提示Access to script at ftp:///xxx.js ...

  2. Webapi 跨域 解决解决错误No 'Access-Control-Allow-Origin' header is present on the requested resource 问题

    首先是web端(http://localhost:53784) 请求 api(http://localhost:81/api/)时出现错误信息: 查看控制台会发现错误:XMLHttpRequest c ...

  3. 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 ...

  4. 解决跨域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 ...

  5. .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 ...

  6. 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 ...

  7. (转)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 ...

  8. ajax跨域(No 'Access-Control-Allow-Origin' header is present on the requested resource)

    问题 在某域名下使用Ajax向另一个域名下的页面请求数据,会遇到跨域问题.另一个域名必须在response中添加 Access-Control-Allow-Origin 的header,才能让前者成功 ...

  9. 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 ...

  10. 跨域问题解决----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 ...

随机推荐

  1. MobaXterm/Xshell快速命令宏配置

    背景:使用MobaXterm或者Xshell连接远程服务器时,添加常用的命令宏一键执行,能提高效率节约时间. 使用方法: MobaXterm: 1)左边菜单栏->Macros->Recor ...

  2. Kafka事务原理剖析

    一.事务概览 提起事务,我们第一印象可能就是ACID,需要满足原子性.一致性.事务隔离级别等概念,那kafka的事务能做到什么程度呢?我们首先看一下如何使用事务 Producer端代码如下 Kafka ...

  3. MIUI12解决安装charles抓包安装证书后还是提示证书不安全

    前言 我抓包这么长时间,这个问题我还是第一次遇到,导致我反复试验,明明安装证书还是提示不安全.我用新买的手机MIUI 12系统弄了半天 解决方案 首先下载证书这部分是一个坑,小米 MIUI 12系统下 ...

  4. 关于linux mint更改资源管理器的快捷键

    前言 首先要知道 linux mint 的默认资源管理器是 nemo 我很不习惯 ctrl+d 在nemo里面是 收藏到侧边栏 我习惯 ctrl+d 在windows上是删除文件 所以下面我就修改这个 ...

  5. python-封装、继承、多态

    封装 面向对象编程有三大特性:封装.继承.多态,其中最重要的一个特性就是封装.封装指的就是把数据与功能都整合到一起,针对封装到对象或者类中的属性,我们还可以严格控制对它们的访问,分两步实现:隐藏与开放 ...

  6. Ubuntu20.04创建快捷方式(CLion)

    打开命令行,创建在桌面上xxx.desktop文件 touch ~/Desktop/Clion.desktop 编辑desktop文件 [Desktop Entry] Encoding=UTF-8 N ...

  7. 【Shell脚本案例】案例6:查看网卡实时流量

    一.背景 监控,对服务器查看实时流量 了解服务器的数据传输量 二.说明 1.获取网络流量 ifconfig查看网卡就能看到数据包传输情况 2.可以使用工具查看 iftop cat /proc/net/ ...

  8. node-sass报错(Node Sass could not find a binding for your current environment)

    解决方案:参考 https://stackoverflow.com/questions/37986800/node-sass-couldnt-find-a-binding-for-your-curre ...

  9. frp内网穿透(已验证)

    # 云服务器frp内网穿透教程 软件源码在https://github.com/fatedier/frp/releases上,根据系统类型,按需下载 教程术语描述 > A 是有固定IP的云服务器 ...

  10. typora软件下载跟安装

    typora软件介绍 typora是一款文本编辑器 是目前非常火爆的文本编辑器 [下载地址](Typora 官方中文站 (typoraio.cn)) 安装操作 pj链接 注意:不要更新!!! 安装 路 ...