今天在学习Angular 的HttpInterceptor 拦截器时,发现添加了新的headers键值之后总是报跨域错误。后台使用的是asp.net core。

检查发现,在添加了新的header之后,每次请求之前都会先发送一个options请求,这个请求被后台拒绝了,所以导致报错。我的后台跨域代码只启用了 AllowAnyOrigin()

方法,所以拒绝了options请求。

为什么会产生options请求呢?参考以下文章,原来是添加了header之后产生非简单请求,所以浏览器会先发送一个options请求到服务器判断一下。

跨域之由Request Method:OPTIONS初窥CORS

由于是被后台拒绝的,所以修改后台代码就可以了。有两种方法

1.修改startup里的ConfigureServices 和 Configure方法

ConfigureServices:

    services.AddCors(options =>
{ options.AddPolicy("cors", p =>
{ p.AllowAnyOrigin();
p.AllowAnyHeader();
p.AllowAnyMethod();
p.AllowCredentials();
});
});

Configure:

 app.UseCors("cors");

2.仅修改Configure方法:

   app.UseCors(cfg =>
{
cfg.AllowAnyOrigin(); //对应跨域请求的地址
cfg.AllowAnyMethod(); //对应请求方法的Method
cfg.AllowAnyHeader(); //对应请求方法的Headers
cfg.AllowCredentials(); //对应请求的withCredentials 值
});

注意对于非简单请求,前三个Allow方法都是必须的。

跨域请求错误: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource的更多相关文章

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

  2. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' heade

    XMLHttpRequest cannot load http://10.164.153.37:8050/WebService/WebService.asmx/wsGetStreetData. Res ...

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

  4. has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

    前端显示: has been blocked by CORS policy: Response to preflight request doesn't pass access control che ...

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

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

  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. ABP PUT、DELETE请求错误405.0 - Method Not Allowed 因为使用了无效方法(HTTP 谓词) 引发客户端错误 No 'Access-Control-Allow-Origin' header is present on the requested resource

    先请检查是否是跨域配置问题,请参考博客:http://www.cnblogs.com/donaldtdz/p/7882225.html 一.问题描述 ABP angular前端部署后,查询,新增都没问 ...

  9. No 'Access-Control-Allow-Origin' header is present on the requested resource——Web Api跨域问题

    最近使用C#写了一个简单的web api项目,在使用项目中的.cshtml文档测试的时候没有任何问题,但是在外部HBuilder上面编写.html通过Ajax调用web api路径时报错: No 'A ...

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

随机推荐

  1. DOM树节点关系

    DOM是JS中专门操作HTML页面内容的 他的三种基本使用方法是: 1.  document.getElementById(''):  ——>选取html页面中带有Id的属性名: 2.docum ...

  2. 关于cubic-bezier 贝塞尔曲线的简单了解

    在animation和transition两个属性中,cubic-bezier是控制变化的速度曲线,主要是生成速度曲线的函数 规定用法是: cubic-bezier(<x1>,<y1 ...

  3. Ubuntu安装rpm

    # sudo apt-get install alien # sudo alien xxx.rpm # sudo dpkg -i xxx.deb

  4. perl自定义简易的面向对象的栈与队列类

    perl中的数组其实已经具备了栈与队列的特点,下面是对数组经过一些封装的stack,queue对象 1.Stack类 创建一个Stack.pm文件 package Stack; sub new{ $s ...

  5. 浅析Java中的集合

    先了解一下集合与数组的区别:数组是java中存储基本数据类型.引用类型的一种容器,但是数组的长度固定,不适合在对象数量未知的情况下使用. 集合只能存储引用类型的数据,长度可变,可在多数情况下使用. 集 ...

  6. Linux学习笔记(十五)用户和用户组

    一.四个配置文件 解释: (1)当组内仅有一个用户且用户名和组名相同时,[组中用户列表]可省略不写 (2)root用户组的组号为0 (3)组号1-499位系统预留组号,是预留给安装在系统中的软件或服务 ...

  7. 开启 clr enabled

    '; GO RECONFIGURE; GO '; GO RECONFIGURE; '; GO

  8. Python自动化测试PO模式

    页面元素定位信息 页面元素定位信息文件 [leadscloud_login] input_user_name = xpath>//*[@id='main']/div/div[2]/div[2]/ ...

  9. 学习Hook的必备知识

    1.汇编 2.API 3.内存 4.进程 5.窗口 必须熟悉的汇编指令: PUSH  入栈 MOV   赋值 JMP(JNZ  JE)  跳转 CALL  调用函数 RET  返回 Cmp  比较 T ...

  10. DataWorks入门

    阿里云有很多成熟的云产品(萌新认知),我自己只用过腾讯云的对象存储,对这类云产品不是特别了解. 有幸参与到大数据相关的项目,跟着学了点工具的使用方法,非常简单,也了解了一些使用大数据分析问题的流程. ...