c# – WCF REST服务模板40(CS)跨域错误
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – WCF REST服务模板40(CS)跨域错误,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4177字,纯文字阅读大概需要6分钟。
内容图文
![c# – WCF REST服务模板40(CS)跨域错误](/upload/InfoBanner/zyjiaocheng/808/d84ac4196635465db70eeb0c1cd85e6a.jpg)
嗨,我有一个WCF休息服务使用(WCF REST服务模板40(CS)).我已设法让它返回Json响应.项目运行时工作正常.但是当我尝试从浏览器向服务进行Ajaxcall时,我收到错误:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading
the remote resource athttp://192.168.0.70:8001/Service/test
.
This can be fixed by moving the resource to the same domain or
enabling 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:
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="ConString" connectionString="SERVER=localhost;DATABASE=fabapp;UID=root;PASSWORD=;"/>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
<modules runAllManagedModulesForAllRequests="true">
<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</modules>
</system.webServer>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<standardEndpoints>
<webHttpEndpoint>
<!--
Configure the WCF REST service base address via the global.asax.cs file and the default endpoint
via the attributes on the <standardEndpoint> element below
-->
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="false" defaultOutgoingResponseFormat="Json"/>
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
</configuration>
我尝试添加crossDomainScriptAccessEnabled =“true”,但是当我这样做时,该服务也无法在localhost上运行.我明白了:
Cross domain javascript callback is not supported in authenticated
services.
我需要在web.config文件中更改什么?
解决方法:
实际上,我已经面临这个问题,我已经更进一步检查WebAPI,当我分析时,需要同样的努力.所以我不得不用WCF修复这个CORS.我会尽量解释一下.开始了.当您使用CrossOrigin访问WCF请求时,例如来自不同域中存在的JS代码,以及来自JS,您尝试执行PUT或POST请求,第一个浏览器发送一个OPTION请求405 HTTP状态,以查看该域是否在允许列表中,然后,如果您的WCF响应OPTIONS请求,发送带有标头值的必需响应,那么浏览器将再次执行POST或PUT请求(之前发布的浏览器),并且它将按预期工作.
注意:您不能发送(“Access-Control-Allow-Origin”,“*”),因为有一个安全功能,它要求在Access-Control-Allow-Origin中列出所需的域名而不是*.
欲了解更多信息 –
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;
}
内容总结
以上是互联网集市为您收集整理的c# – WCF REST服务模板40(CS)跨域错误全部内容,希望文章能够帮你解决c# – WCF REST服务模板40(CS)跨域错误所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。