Irf*_*eli 5 asp.net wcf jquery wcf-security
我在使用JQUERY AJAX消费WCF服务时遇到问题。我知道这是跨域问题,并且已经阅读了很多解决方案。但没有一个对我有用。以下是所有相关代码。有人可以帮我吗?
谢谢
[OperationContract]
[WebInvoke(Method = "POST",BodyStyle=WebMessageBodyStyle.Bare,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
[return: MessageParameter(Name = "result")]
public ServiceSearchResponse GetSearchResults(ServiceSearchParams sParams)
{
/// search function
}
Run Code Online (Sandbox Code Playgroud)
JQUERY:
$.ajax({
type: 'POST',
url: "http://myserviceurl.com/GetSearchResults",
data: p,
contentType: "application/json; charset=utf-8",
dataType: 'json',
crossDomain: true,
success: function (data) {
alert(data);
},
failure: function (response) {
alert('failed');
},
error: function (response) {
alert(JSON.stringify(response));
}
});
Run Code Online (Sandbox Code Playgroud)
Webconfig:
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
</customHeaders>
</httpProtocol>
</system.webServer>
<system.serviceModel>
<protocolMapping>
<add scheme="http" binding="webHttpBinding" bindingConfiguration="" />
</protocolMapping>
<behaviors>
<serviceBehaviors>
<behavior name="DefaultServiceBehavior">
<!--Added DefaultServiceBehavior referenced at service tag above-->
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="myserives.services.AppServicesAspNetAjaxBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<services>
<service name="mypackage.services.MyServices">
<endpoint address="" behaviorConfiguration="myserives.services.AppServicesAspNetAjaxBehavior" binding="webHttpBinding"
bindingConfiguration="LargeSizeMessages" contract="myContractName" />
</service>
</services>
<bindings>
<webHttpBinding>
<binding name="LargeSizeMessages" maxBufferSize="2147483647"
maxBufferPoolSize="2147483647"
maxReceivedMessageSize="2147483647" crossDomainScriptAccessEnabled="true">
<security mode="None" />
</binding>
</webHttpBinding>
</bindings>
</system.serviceModel>
Run Code Online (Sandbox Code Playgroud)
这是这样的:http : //screencast.com/t/b7tsqld6
看到错误:http : //screencast.com/t/pWQNAlmMYS3
在发布数据中什么也没有。虽然即时贴发布数据。
-更新
看到我的Global.asax ..我在这里做错了:
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.Cache.SetNoStore();
EnableCrossDmainAjaxCall();
}
private void EnableCrossDmainAjaxCall()
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin",
"*");
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods",
"GET, POST");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Credentials",
"true");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers",
"Content-Type, Accept");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age",
"1728000");
HttpContext.Current.Response.End();
}
}
Run Code Online (Sandbox Code Playgroud)
我会尝试在 web.config 中设置Access-Control-Allow-Credentials: true。还可以尝试将Access-Control-Allow-Headers设置为与您的 jquery ajax 调用匹配的标头。应该是“application/json”,但请检查 fiddler (或类似的)以确定。最后尝试设置Access-Control-Request-Method: POST
我遇到了类似的问题,在尝试了 web.config 的不同设置后,我设法找到了实际有效的组合。祝你好运 :)
更新
另外,我会确保 access-control-allow-credentials 与其他参数采用相同的大小写形式。即访问控制允许凭证。我真的没有任何这方面的资料,但以防万一:)
我会在不使用本地主机的情况下尝试这个。使用正确的主机名在不同的服务器上设置测试环境。至少 Chrome 在处理本地主机上的 cors 时遇到了问题。
根据http://www.html5rocks.com/en/tutorials/cors/,请求的内容类型应该是以下之一:
更新2
您可以尝试更多的事情:添加:
$.support.cors = true;
Run Code Online (Sandbox Code Playgroud)
之前
$.ajax({ ...
Run Code Online (Sandbox Code Playgroud)
我认为问题可能是由不正确的内容类型引起的。当我对 Sharepoint 2013 服务器进行 CORS 设置时,我添加了以下内容并且一切正常:
headers: {
"accept": "application/json;odata=verbose;charset=utf-8",
"content-type":"application/json;odata=verbose;charset=utf-8"
},
Run Code Online (Sandbox Code Playgroud)
该内容类型可能与您无关,但这样指定可能很重要。