Tag: cors

为什么添加额外的标头使AJAX调用失败

AJAX电话: $.ajax({ url: “http://myserver2:296/api/Demo/HelloWorld”, type: “GET”, dataType: ‘JSONP’, jsonp: “callback”, headers: { ‘API_KEY’: ‘mykey09090’ }, success: function (result) { console.log(result); }, error: ajaxFailed }); function ajaxFailed(xmlRequest) { alert(xmlRequest.status + ‘ \n\r ‘ + xmlRequest.statusText + ‘\n\r’ + xmlRequest.responseText); } 我收到以下错误: Failed to load resource: the server responded with a status of 403 (Forbidden) 。 但是当我使用Postman时,我只需要添加带有http://myserver2:296/api/Demo/HelloWorld […]

c#已启用CORS的Web Api和所请求资源上存在可怕的“Access-Control-Allow-Origin”标头

我有一个C#Web API项目。 我使用Web API 2标准创建了一个Controller。 我试图只在一个动作上启用CORS,这就是我所拥有的: namespace MyProject.Controllers { public class MyControllerController : ApiController { // POST api/mycontroller [EnableCors(“http://link.myurl.com”, “*”, “*”)] public bool Post(MyCustomObject customObject) { // Function that returns Bool return myFunction(customObject); } } } 我的WebApiConfig.cs看起来像这样: // Web API configuration and services config.EnableCors(); // Web API routes config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: “DefaultApi”, routeTemplate: “api/{controller}/{id}”, defaults: […]

让CORS与Nancy合作

我试图让所有类型的请求与Nancy和CORS一起工作。 目前我在请求结束时添加了一个管道: pipelines.AfterRequest.AddItemToEndOfPipeline((ctx) => ctx.Response .WithHeader(“Access-Control-Allow-Origin”, “http://localhost:57515”) .WithHeader(“Access-Control-Allow-Methods”, “POST, GET, DELETE, PUT, OPTIONS”) .WithHeader(“Access-Control-Allow-Headers”, “Accept, Origin, Content-type”) .WithHeader(“Allow”, “POST, GET, DELETE, PUT, OPTIONS”)) 选项请求返回状态代码为200,这使我相信它执行正常,但对于OPTIONS以外的任何类型的请求,它都会因405 Method Not Allowed而失败。 还有什么我需要做客户端或服务器端才能使其工作? 我使用的客户端库是骨干。 提前致谢。

ASP.NET 5:响应中的Access-Control-Allow-Origin

根据我的理解,当相应地启用CORS时,响应模型应包括以下头信息(假设我想允许所有内容): Access-Control-Allow-Origin: * Access-Control-Allow-Method: * Access-Control-Allow-Header: * 在Startup启用它: public void ConfigureServices(IServiceCollection services) { //… services.AddCors(); services.ConfigureCors(options => { options.AddPolicy(“AllowAll”, p => p.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader().AllowCredentials()); }); //… } public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { //… app.UseCors(“AllowAll”); //… } 问题是这些头文件都没有返回,并且在尝试从API请求时出现以下错误: 对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。 因此不允许Origin’http:// localhost ‘访问。

为Web API 1,.net 4.0启用CORS

我需要为我的Web API启用CORS,目前我无法升级到Framework 4.5。 (我了解System.Web.Http.Cors.EnableCorsAttribute。) 我试图将以下内容添加到我的Web.config中以查看它是否有效,但它没有: 我还试图通过使用System.Web.Http.Filters.ActionFilterAttribute手动将Access-Control-Allow-Origin标头设置为“*”(基于这篇文章: 向Web API中的所有响应添加自定义标头 ) -但由于请求在进入动作过滤之前被拒绝,因此无法解决问题。 所以我现在有点卡住..任何帮助都表示赞赏。 编辑:结果 一直都是答案,我在测试之前一定做错了。 但是这个解决方案意味着所有操作都启用了CORS(现在可以使用)。

WCF DataService(OData)和CORS

我正在尝试使用跨域请求的WCF DataService。 我发现这有关如何使用CORS的WCF服务: http : //blogs.microsoft.co.il/blogs/idof/archive/2011/07/02/cross-origin-resource-sharing-cors-and -wcf.aspx 我下载了示例,但无法使用DataService。 它适用于示例服务,但不适用于我的DataService。 这是我非常简单的WCF DataService: public class TestService : DataService { public static void InitializeService(DataServiceConfiguration config) { config.UseVerboseErrors = true; config.SetEntitySetAccessRule(“Items”, EntitySetRights.All); config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3; } } TestService.svc文件: DataContext也很简单: public class DataContext : DbContext { public DbSet Items { get; set; } } 但是,预检选项请求返回的是501.我是否缺少让CORS与Dataservice一起工作?

ASP.NET中CORS的问题

我有这个应用程序,我想在Web.Config中设置我的自定义标题,唉,这并不总是万无一失。 上面的设置和它的迭代如 在所有场景中都没有为我工作过。 截至目前,此设置适用于大约50%的测试机器,并且在其他测试机器中提供405 Method Not Allowed 。 替代方法在WebApiConfig.cs设置,并取消注释Web.config的自定义标头。 //Web API Cross origin requests – Enable var cors = new EnableCorsAttribute(“*”, “*”, “*”); config.EnableCors(cors); 为什么这里有这么多含糊不清的地方?我怎么知道CORS一直在哪里工作? 我真的很感兴趣在Web.config上设置CORS,因为我希望在部署版本中灵活地修改它。

.NET Core中的CORS

我试图以这种方式在.NET Core中启用CORS: public IConfigurationRoot Configuration { get; } public void ConfigureServices(IServiceCollection services) { services.AddCors(options => options.AddPolicy(“AllowAll”, p => p.AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader())); services.AddMvc(); } public void Configure(IApplicationBuilder app) { app.UseCors(“AllowAll”); app.UseMvc(routes => { routes.MapRoute( name: “default”, template: “{controller=Home}/{action=Index}/{id?}”); }); } } 但是,当我通过Angular 2向我的应用程序发送请求时,我得到了名人 “请求的资源上没有’Access-Control-Allow-Origin’标头。” 错误信息。 我也在使用Windows身份validation+ WebListener。 如果我与邮递员核对,唯一的响应标题是: Content-Length→3533 Content-Type→application / json; charset = utf-8日期→2016年10月14日星期五12:17:57 GMT服务器→Microsoft-HTTPAPI […]

CORS:凭证模式是’包含’

是的,我知道你在想什么 – 还有另一个CORS问题,但这次我很难过。 所以要开始,实际的错误信息: XMLHttpRequest无法加载http://localhost/Foo.API/token 。 当请求的凭据模式为“include”时,响应中“Access-Control-Allow-Origin”标头的值不能是通配符“* ” 。 因此不允许来源’ http:// localhost:5000 ‘访问。 XMLHttpRequest发起的请求的凭据模式由withCredentials属性控制。 我不确定凭证模式的含义是’包含’ ? 所以当我在邮递员中执行请求时,我没有遇到这样的错误: 但是,当我通过我的angularjs网络应用程序访问相同的请求时,我被这个错误困扰了。 这是我的angualrjs请求/回复。 你会看到响应是OK 200 ,但我仍然收到CORS错误: 小提琴请求和响应: 下图演示了Web前端到API的请求和响应 所以基于我在网上阅读的所有其他post, 看起来我做的是正确的,这就是为什么我无法理解错误。 最后,这是我在angualrjs(登录工厂)中使用的代码: API中的CORS实现 – 参考目的: 方法1使用: public static class WebApiConfig { public static void Register(HttpConfiguration config) { EnableCrossSiteRequests(config); } private static void EnableCrossSiteRequests(HttpConfiguration config) { var cors = new EnableCorsAttribute(“*”, […]

选项405(方法不允许)web api 2

我创建了一个web api 2,我正在尝试对它进行跨域请求但是我收到以下错误: 选项http://www.example.com/api/save 405(方法不允许) 我已经浏览了一下这个问题的大多数解决方案都说我需要从NuGet安装COR并启用它,所以我已经安装了包并标记了我的控制器 [EnableCors(“*”, “*”, “*”)] 但这仍然没有解决问题。 我的ApiController只有以下Save方法: [ResponseType(typeof(int))] public IHttpActionResult Save(Student student) { if (ModelState.IsValid) { using (StudentHelper helper = new StudentHelper()) { return Ok(helper.SaveStudent(student)); } } else { return BadRequest(ModelState); } } 这是我来自不同领域的js: $.ajax({ type: “POST”, crossDomain: true, data: JSON.stringify(student), crossDomain: true, url: ‘http://www.example.com/api/save’, contentType: “application/json”, success: function (result) { […]