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:22852/api/values,
What is "Same Origin"?
Two URLs have the same origin if they have identical schemes, hosts, and ports. (RFC 6454)
These two URLs have the same origin:
http://example.com/foo.html
http://example.com/bar.html
These URLs have different origins than the previous two:
http://example.net
- Different domainhttp://example.com:9000/foo.html
- Different porthttps://example.com/foo.html
- Different schemehttp://www.example.com/foo.html
- Different subdomain
http://localhost:23160/HtmlPage.html 代码如下:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="Scripts/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
$(function () {
$("#Button1").click(function () {
$.ajax({
url: "http://localhost:22852/api/values",
success: function (data) {
alert(data);
}
});
});
});
</script>
</head>
<body>
<input id="Button1" type="button" value="button" />
</body>
</html>
通过调试我们发现,ajax请求成功发送到API,API后台的断点走到了,并且返回了数据,并没有什么问题。 在web端,查看网络,显示的status状态也是200,但是响应的内容却是空的。
这时,查看控制台会发现错误:XMLHttpRequest cannot load http://localhost:22852/api/values. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:23160' is therefore not allowed access.
提示API响应头中没有‘Access-Control-Allow-Origin'头,标识可以允许源http://localhost:23160访问。
我们发现,同源策略,并没有阻止请求的发送,但是阻止了,不同源请求的响应。
如何对指定的源,开放访问?
微软的工程师已经给出了解决方案,直接在安装一个现成的package就能解决。
1.add the CORS NuGet package. In Visual Studio, from the Tools menu, select Library Package Manager, then select Package Manager Console. In the Package Manager Console window, type the following command:
Install-Package Microsoft.AspNet.WebApi.Cors
This command installs the latest package and updates all dependencies, including the core Web API libraries. User the -Version flag to target a specific version. The CORS package requires Web API 2.0 or later.
2.Open the file App_Start/WebApiConfig.cs. Add the following code to the WebApiConfig.Register method.
using System.Web.Http;
namespace WebService
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// enable cors
config.EnableCors(); config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
}
3. Next, add the [EnableCors] attribute to the TestController
class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Cors; namespace ServerSide.Controllers
{
[EnableCors(origins: "http://localhost:23160", headers: "*", methods: "*")]
public class TestController : ApiController
{
public HttpResponseMessage Get()
{
return new HttpResponseMessage()
{
Content = new StringContent("GET: Test message")
};
}
}
}
经过以上3个步骤,在web端调用API后,成功获取响应内容:”GET: Test message“
详细请参考:http://www.asp.net/web-api/overview/formats-and-model-binding/media-formatters
我走的弯路:
1.可能是VS2013的原因,安装了Microsoft.AspNet.WebApi.Cors包之后,生成成功,运行的时候,报错:未能加载文件或程序集“System.Web.Http”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。我一看,是因为引用的版本不一致,但是packages中和Bin中都是存放的最新的5.2.3版本的,只是却报错说找不到5.0.0版本,很奇怪,5.0.0版本是安装Cors包之前版本,安装cors包会把依赖的system.web.http自动升级到5.2.3,把l5.0.0的老版本删掉了。为什么还是报这个错误。而且很奇怪的是,web.config中并没有dll引用的配置,package.config中也没有,以前.net项目,dll的版本维护都是在web.config中,难道WebAPI换了方式,那是怎么配置的呢,网上也没有找到答案,后来我查看我本地其它API项目,发现dll是在web.config中配置的呀。 于是尝试按照其它项目一样,在webconfig中configuration根目录添加runtime阶段配置assemblyBinding,指定5.2.3版本的dll,成功解决问题,web.config代码如下:
<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=301879
-->
<configuration>
<appSettings>
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web> <system.webServer>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
</dependentAssembly>
</assemblyBinding>
</runtime> </configuration>
2.上面的问题解决后,忘记了在App_Start/WebApiConfig.cs中添加配置,仅仅在Controller类上添加了attribute,还是不能跨源访问,以为是我VS哪里出问题了,我的版本是VS2013,作者是VS2013 Update2,又尝试去升级。最后重新走一遍流程,才发现遗漏配置了,添加配置后,终于能够跨源请求,正确获取响应了。
As.net WebAPI CORS, 开启跨源访问,解决错误No 'Access-Control-Allow-Origin' header is present on the requested resource的更多相关文章
- Webapi 跨域 解决解决错误No 'Access-Control-Allow-Origin' header is present on the requested resource 问题
首先是web端(http://localhost:53784) 请求 api(http://localhost:81/api/)时出现错误信息: 查看控制台会发现错误:XMLHttpRequest c ...
- 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 ...
- 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 ...
- 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 ...
- .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 ...
- 解决跨域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 ...
- (转)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,才能让前者成功 ...
- 跨域问题解决----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 ...
随机推荐
- U盘加载硬盘控制卡驱动安装Windows 2003 指南
http://www.dell.com/Support/Article/cn/zh/cnbsd1/SLN263067
- JS 中 this 关键字详解
本文主要解释在JS里面this关键字的指向问题(在浏览器环境下). 首先,必须搞清楚在JS里面,函数的几种调用方式: 普通函数调用 作为方法来调用 作为构造函数来调用 使用apply/call方法来调 ...
- Android IOS WebRTC 音视频开发总结(五八)-- 图文解说视频直播原理
本文主要介绍rtmp&hls视频直播原理,文章最早发表在我们的微信公众号上,详见这里,欢迎关注微信公众号blackerteam,更多详见www.blackerteam.com 现在视频直播很火 ...
- Android WebRTC 音视频开发总结(四)-- webrtc传输模块
在介绍WebRTC通讯之前我们先来看一个P2P视频聊天包括的主要过程,转载请说明出处(博客园RTC.Blacker): 音视频数据采集->编码->发送->接收->解码-> ...
- Jetson TK1 Restore 步骤
Jetson TK1 Restore 步骤 下载驱动包和文件系统包: 1:驱动包 2:文件系统 具体参见文档:http://download.csdn.net/detail/xiabodan/7 ...
- 实现Win7远程桌面关机和重启
通过远程桌面控制Win7系统时,菜单中没有关机和重启按钮, 1.方法1 关机 shutdown -s -t 0重启 shutdown -r -t 0 可以先打开运行框(Win+R键),输入上述命令即可 ...
- 当数据0跟if判断冲突的时候
我是很无奈的,以后都要2,3,4,5这样去标志状态: 分配状态:<select name="is_send" > <option selected="s ...
- 画画板--第三方开源--DrawableView
Android上的第三方开源DrawableView支持手写,类似于写字板.DrawableView支持改变画笔颜色,画笔线条粗细,画布的手势缩放和拖曳显示部分区域.并最终支持将手绘的图保存到本地.在 ...
- jQuery操作 input type=checkbox的实现代码
代码如下: <input type="checkbox">: 2012欧洲杯"死亡之组"小组出线的国家队是:<br> <input ...
- Spark官方文档——本地编写并运行scala程序
快速开始 本文将介绍如何用scala.java.python编写一个spark单击模式的程序. 首先你只需要在一台机器上成功建造Spark:做法: 进入Spark的根目录,输入命令:$ sbt/sbt ...