WCF REST服务模板40(CS)跨域错误

嗨,我有一个WCFrest服务使用(WCF REST服务模板40(CS))。 我已设法让它返回Json响应。 项目运行时工作正常。 但是当我尝试从浏览器向服务进行Ajaxcall时,我收到错误:

跨源请求已阻止:同源策略不允许在http://192.168.0.70:8001/Service/test读取远程资源。 这可以通过将资源移动到同一域或启用CORS来解决。

和Ajax电话:

 $.ajax({ type: "POST", url: "http://192.168.0.70:8001/Service/test", contentType: "application/json; charset=utf-8", dataType: "json", async: true, cache: false, success: function (msg) { var Tabels = msg.d; alert('success'); } }); 

这是web.config:

                        <!-- Configure the WCF REST service base address via the global.asax.cs file and the default endpoint via the attributes on the  element below -->      

我尝试添加crossDomainScriptAccessEnabled =“true”,但是当我这样做时,该服务也无法在localhost上运行。 我明白了:

经过身份validation的服务不支持跨域javascript回调。

我需要在web.config文件中更改什么?

实际上,我已经面临这个问题,我已经更进一步检查WebAPI,当我分析时,需要同样的努力。 所以我不得不CORS with WCF修复这个CORS with WCF 。 我会尽力解释一下。 开始了。 当您使用CrossOrigin访问WCF请求时,例如来自不同域中存在的JS代码,以及来自JS,您尝试执行PUTPOST请求,第一个浏览器发送OPTION请求405 HTTP Status ,以查看此域是否在允许列表中,然后,如果您的WCF响应OPTIONS请求,发送带有标头值的必需响应,那么浏览器将再次执行POSTPUT请求(之前发布的浏览器),并且它将按预期工作。

注意:您无法发送("Access-Control-Allow-Origin", "*") ,因为有一个安全function,它要求在Access-Control-Allow-Origin而不是*列出所需的域名。

欲了解更多信息 –

http://social.msdn.microsoft.com/Forums/ro-RO/5613de55-2573-49ca-a389-abacb39e4f8c/wcf-rest-service-post-cross-domain-not-working?forum=wcf

https://stackoverflow.com/questions/26163802/wcf-cors-request-from-jquery-not-working

从实际经验来看,我已尝试*在该标题中,它无法正常工作。 如果你不相信我,请继续尝试。

最后代码如下。 你需要把它放在Global.asax

 protected void Application_BeginRequest(object sender, EventArgs e) { String domainname = HttpContext.Current.Request.Headers["Origin"].ToString(); if (IsAllowedDomain(domainname)) HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", domainname); String allowedmethods = "POST, PUT, DELETE, GET"; String headers = HttpContext.Current.Request.Headers["Access-Control-Request-Headers"].ToString(); String accesscontrolmaxage = "1728000"; String contenttypeforoptionsrequest = "application/json"; if (HttpContext.Current.Request.HttpMethod == "OPTIONS") { //These headers are handling the "pre-flight" OPTIONS call sent by the browser HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", allowedmethods); HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", headers); HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", accesscontrolmaxage); HttpContext.Current.Response.AddHeader("ContentType", contenttypeforoptionsrequest); HttpContext.Current.Response.End(); } } private bool IsAllowedDomain(String Domain) { if (string.IsNullOrEmpty(Domain)) return false; string[] alloweddomains = "http://192.168.0.70:8001"; // you can place comma separated domains here. foreach (string alloweddomain in alloweddomains) { if (Domain.ToLower() == alloweddomain.ToLower()) return true; } return false; } 

将以下内容放在wcf应用程序的global.asax文件中

 protected void Application_BeginRequest(object sender, EventArgs e) { HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*"); if (HttpContext.Current.Request.HttpMethod == "OPTIONS") { HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache"); HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST"); HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept"); HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000"); HttpContext.Current.Response.End(); } } 

您可能需要查看此链接中的post:

http://www.codeproject.com/Articles/223572/Calling-Cross-Domain-WCF-service-using-Jquery-Java

Ajax允许在后台获取数据而不会干扰显示。 通过使用XMLHttpRequest对象完成的Ajax调用允许客户端JavaScript代码进行HTTP连接。 但是由于浏览器施加的限制,Ajax调用不允许从跨域获取数据。 从其他域请求数据时会发出安全性错误。 避免安全性错误的一种方法是控制数据所在的远程服务器,并且每个请求都转到同一个域。 这引出了一个问题,如果数据仅来自自己的服务器,那有什么乐趣呢? 如果需要从其他服务器获取数据该怎么办?

有一种方法可以摆脱这种限制 – 在Web页面中插入一个动态脚本元素,其源代码指向另一个域中的服务URL,并在脚本本身中获取数据。 当脚本加载时,它会执行。 它的工作原理是因为同源策略不会阻止动态脚本插入并将脚本视为从提供Web页面的域加载它们。 但是,如果此脚本尝试从另一个域加载文档,则它将失败。 幸运的是,您可以通过添加JavaScript Object Notation(JSON)来改进此技术。

JSONP或“带填充的JSON”是对基本JSON数据格式的补充,这是一种允许页面从不同域中的服务器请求数据的使用模式。 作为此问题的解决方案,JSONP是一种称为跨源资源共享的更新方法的替代方案。

在相同的原始策略下,server1.example.com提供的网页通常无法连接到server1.example.com以外的服务器或与之通信。 HTML元素是一个例外。 利用元素的开放策略,一些页面使用它们来检索JavaScript代码,这些代码对来自其他来源的动态生成的JSON格式数据进行操作。 此使用模式称为JSONP。 对JSONP的请求不是JSON,而是任意JavaScript代码。 它们由JavaScript解释器评估,而不是由JSON解析器解析。

就个人而言,我以前遇到过这个问题,但接下来我改变了架构。 我通常只是从我的Web服务器调用WCF。 所以

来自浏览器的AJAX请求 – > Web服务器 – > WCF服务

我认为出于安全考虑,此限制旨在防止许多安全漏洞。 但是,如果您真的想要实现您想要的并且以前的答案不符合您的要求,您可能需要查看此post作为替代方案。 看起来很有希望。 请让我更新。

希望能帮助到你。 谢谢。