最近使用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跨域问题的更多相关文章

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

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

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

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

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

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

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

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

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

随机推荐

  1. redhat 7.6 配置repo源

    vi /etc/yum.repos.d/base.repo          #编辑配置repo配置文件,如果没有则自动创建,没有影响 name=base     //源名字,起什么名都没影响 bas ...

  2. SpringMVC 接收表单数据、数据绑定、解决请求参数中文乱码

    接收表单数据有3种方式. 1.使用简单类型接收表单数据(绑定简单数据类型) 表单: <form action="${pageContext.request.contextPath}/u ...

  3. 5.使用Redis+Flask维护动态Cookies池

    1.为什么要用Cookies池? 网站需要登录才可爬取,例如新浪微博 爬取过程中如果频率过高会导致封号 需要维护多个账号的Cookies池实现大规模爬取 2.Cookies池的要求 自动登录更新 定时 ...

  4. mysql 优化策略(如何利用好索引)

    博客园 首页 联系 管理 随笔- 3282  文章- 0  评论- 157  mysql联合索引 命名规则:表名_字段名1.需要加索引的字段,要在where条件中2.数据量少的字段不需要加索引3.如果 ...

  5. 基于金山快盘的Git服务器、快盘+ Git GUI 实现代码版本管理

        Git,这货堪称神器,用了它就再也不想用其他VCS了,就像上了高速就不想再走国道一样. Git的强大之处在于,你可以在局域网内的任何一个共享路径下创建仓库,而不需要运行任何服务.所有的操作都是 ...

  6. CentOS6.9安装redis

    目录 Centos6.9下的Redis安装和配置(最简易方式) redis客户端登录方式 Centos6.9下的Redis安装和配置(最简易方式) 在服务器上创建一个目录/service,然后下载re ...

  7. layer 点击yes后在回调函数里获取layer.open({})iframe里面元素

    参考:http://fly.layui.com/jie/19690/ yes: function(index, layero) { uid.value = $(layero).find('iframe ...

  8. Linux系统资深运维工程师的进阶秘籍

    2010年毕业,从事IT行业已经接近7个年头,一路走来有很多不足,不论是技术上的还是工作当中的待人接事等,但正是这些不足让我有了现在的进步,技术上从最初的做水晶头,综合布线到服务器上架,网络设备调试, ...

  9. 第1节 kafka消息队列:10、flume与kafka的整合使用

    11.flume与kafka的整合 实现flume监控某个目录下面的所有文件,然后将文件收集发送到kafka消息系统中 第一步:flume下载地址 http://archive.cloudera.co ...

  10. class中static总结-静态成员函数和静态成员变量

    C++规定const静态类成员可以直接初始化,其他非const的静态类成员需要在类声明以外初始化,我们一般选择在类的实现文件中初始化,初始化的方式是书写一遍类型的定义: //A.cpp ); //使用 ...