No 'Access-Control-Allow-Origin' header is present on the requested resource——Web Api跨域问题
最近使用C#写了一个简单的web api项目,在使用项目中的.cshtml文档测试的时候没有任何问题,但是在外部HBuilder上面编写.html通过Ajax调用web api路径时报错:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
导致这个问题的原因是在跨域访问web api的api时,C#自带的web api并不能支持跨域访问,如果需要,可以更改配置来实现。
1、更改Web.config文件,加上如下代码
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*"/>
<add name="Access-Control-Allow-Headers" value="Content-Type,Token" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
</customHeaders>
</httpProtocol>
配置了Web.config文件后,平常使用的GET,POST可以使用,但是使用PUT请求的时候还是会报405的错,即method not allow,然后需要配置Global.asax文件
插入如下代码:
protected void Application_BeginRequest()
{
if (Request.Headers.AllKeys.Contains("Origin") && Request.HttpMethod == "OPTIONS")
{
Response.End();
}
}
配置这两个文件之后,web api就可以跨域访问了。
No 'Access-Control-Allow-Origin' header is present on the requested resource——Web Api跨域问题的更多相关文章
- 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 ...
- WCF REST开启Cors 解决 No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. The response had HTTP status code 405.
现象: 编写了REST接口: [ServiceContract] public interface IService1 { [OperationContract] [WebInvoke(UriTemp ...
- Failed to load http://wantTOgo.com/get_sts_token/: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://fromHere.com' is therefore not allowed access.
Failed to load http://wantTOgo.com/get_sts_token/: No 'Access-Control-Allow-Origin' header is presen ...
- 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 ...
- .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 ...
- No 'Access-Control-Allow-Origin' header is present on the requested resource.
今天做一个AJAX案例时,浏览器监控到如下错误: XMLHttpRequest cannot load http://54.169.69.60:8081/process_message. No 'Ac ...
随机推荐
- 一个DBA的雄心壮志
math-liu, 一个甲方的DBA,同时他还有另外一个名字“怕提神”.在某甲方工作的他最近这几年如打了鸡血一般工作起来完全由拼命三郎的那股劲,据说能唯一使他感到一丝鼓励和安慰的是以下这两位大牛前辈. ...
- Django学习 之 HTTP与WEB为Django做准备
一.HTTP 1.HTTP 简介 HTTP协议(HyperText Transfer Protocol,超文本传输协议)是因特网上应用最为广泛的一种网络传输协议,所有的WWW文件都必须遵守这个标准. ...
- 卸载sql server 2008
一. SQL2008卸载. 1.从控制面板卸载 1)点击计算机右下角“开始”,点击“控制面板” 2)点击“卸载程序”. 3)在程序列表中找到“Microsoft SQL Server 2008” ...
- 记录一次Git远程仓库版本回退
操作过程: 首先查看远程仓库版本,如下图所见,最近一次提交为2018-03-19 22:16:25 第一步:使用git log命令查看历史提交记录,选择要回退的版本号,commit后面一串字符,这里我 ...
- VS2008 error PRJ0002 : 错误的结果 31 (从“C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\rc.exe”返回)。
解决方案,选择属性->配置属性->清单工具->输入和输出->嵌入清单,把是改成否
- 插入和查询HBase速度都比较慢
表层问题:插入和查询HBase速度比较慢 排查一,查看HBase节点状态,发现正常运行: 排查二,查看访问HBase服务的状态,发现服务停止: 依次点击服务实例,查看服务状态 133和135节点上的服 ...
- JDBC和连接池
JDBC 所有的数据库操作框架都是用在JDBC的基础上做多次封装的,因为JDBC的操作很复杂 引入Jar包 连接数据库操作 书写sql语句,传参 查询,取值 关闭连接 //1.注册驱动(静态方法)(包 ...
- Day2-L-棋盘问题-POJ1321
在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C. ...
- 《Netlogo多主体建模入门》笔记4
4- 从Langton的蚂蚁看Turtle与Patch的交互 这只蚂蚁从10000步开始,就会自发地 “建桥” Turtle与Patch就好比是,一个方块和一个格子的关系. 一个格子上可以 ...
- win10下python3安装深度学习一般要用的库
matplotlib :绘图库 seaborn:基于matplotlib的图形可视化包 numpy:函数.矩阵运算库 pandas :基于numpy的结构化数据分析库 首先看一下cmd能不能使用pip ...