C# 通过Ajax(CORS)从ASP.NET MVC调用WCF服务
我有一个在端口(25537)上运行的WCF服务,还有一个MVC应用程序。从MVC应用程序,我对WCF服务进行post调用 我得到一个错误: 访问控制允许标头不允许选项请求标头字段内容类型 我尝试了很多选择,但没有任何改变 到Global.asaxC# 通过Ajax(CORS)从ASP.NET MVC调用WCF服务,c#,jquery,ajax,wcf,asp.net-mvc-4,C#,Jquery,Ajax,Wcf,Asp.net Mvc 4,我有一个在端口(25537)上运行的WCF服务,还有一个MVC应用程序。从MVC应用程序,我对WCF服务进行post调用 我得到一个错误: 访问控制允许标头不允许选项请求标头字段内容类型 我尝试了很多选择,但没有任何改变 到Global.asax protected void Application_BeginRequest(object sender, EventArgs e) { Response.AppendHeader("Access-Contro
protected void Application_BeginRequest(object sender, EventArgs e)
{
Response.AppendHeader("Access-Control-Allow-Origin", "*");
Response.AppendHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
if (HttpContext.Current.Request.HttpMethod == "OPTIONS" )
{
Response.AppendHeader("Access-Control-Allow-Methods", "GET, POST");
Response.AppendHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
Response.AppendHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.End();
}
}
到Web.Config
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept" />
</customHeaders>
</httpProtocol>
没有什么变化
有什么想法吗
提前感谢。您应该在js端和wcf端进行一些配置 关于js:
function post(type, data, timeout) {
return $.ajax({
url: createUrl(POST, type),
type: POST,
data: JSON.stringify(data),
contentType: "application/json",
crossDomain: true,//<<<<-------------It`s here!
processData: false,
dataType: "json",
timeout: timeout
});
}
函数post(类型、数据、超时){
返回$.ajax({
url:createUrl(帖子,类型),
类型:POST,,
数据:JSON.stringify(数据),
contentType:“应用程序/json”,
crossDomain:true,//要跨域访问WCF服务,您必须允许绑定使用crossDomainScriptAccessEnabled=“true”
我正在分享一个非常好的博客,我相信,这将帮助您了解跨域访问WCF服务以及jQueryAjax和Jsonp方法之间的区别
您是否将WCF服务的CustomHeader放在web.config中?今天(不是我的博客)有一个关于使用CORS设置WCF的文章。您可能会发现它很有用
<system.serviceModel>
<extensions>
<behaviorExtensions>
<add name="crossOriginResourceSharingBehavior" type="Nelibur.ServiceModel.Behaviors.EnableCrossOriginResourceSharingBehavior, Nelibur" />
</behaviorExtensions>
</extensions>
...
<services>
<service name="Nelibur.ServiceModel.Services.Default.JsonServicePerCall">
<host>
<baseAddresses>
<add baseAddress="http://127.0.0.1:61111/webhost" />
<add baseAddress="https://127.0.0.1:61112/webhost" />
</baseAddresses>
</host>
<endpoint binding="webHttpBinding"
contract="Nelibur.ServiceModel.Contracts.IJsonService" behaviorConfiguration="jsonBehavior" bindingConfiguration="webHttpBinding"/>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="jsonBehavior">
<webHttp />
<crossOriginResourceSharingBehavior />
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
using System;
using System.Collections.Generic;
using System.ServiceModel.Channels;
using System.ServiceModel.Configuration;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
namespace Nelibur.ServiceModel.Behaviors
{
/// <summary>
/// Provides CORS support.
/// </summary>
public class EnableCrossOriginResourceSharingBehavior : BehaviorExtensionElement, IEndpointBehavior
{
/// <summary>
/// Gets the type of behavior.
/// </summary>
/// <returns>
/// A <see cref="T:System.Type" />.
/// </returns>
public override Type BehaviorType
{
get { return typeof (EnableCrossOriginResourceSharingBehavior); }
}
/// <summary>
/// Implement to pass data at runtime to bindings to support custom behavior.
/// </summary>
/// <param name="endpoint">The endpoint to modify.</param>
/// <param name="bindingParameters">The objects that binding elements require to support the behavior.</param>
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
}
/// <summary>
/// Implements a modification or extension of the client across an endpoint.
/// </summary>
/// <param name="endpoint">The endpoint that is to be customized.</param>
/// <param name="clientRuntime">The client runtime to be customized.</param>
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
}
/// <summary>
/// Implements a modification or extension of the service across an endpoint.
/// </summary>
/// <param name="endpoint">The endpoint that exposes the contract.</param>
/// <param name="endpointDispatcher">The endpoint dispatcher to be modified or extended.</param>
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
var requiredHeaders = new Dictionary<string, string>();
requiredHeaders.Add("Access-Control-Allow-Origin", "*");
requiredHeaders.Add("Access-Control-Request-Method", "POST,GET,OPTIONS");
requiredHeaders.Add("Access-Control-Allow-Headers", "X-Requested-With,Content-Type");
endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new CustomHeaderMessageInspector(requiredHeaders));
}
/// <summary>
/// Implement to confirm that the endpoint meets some intended criteria.
/// </summary>
/// <param name="endpoint">The endpoint to validate.</param>
public void Validate(ServiceEndpoint endpoint)
{
}
/// <summary>
/// Creates a behavior extension based on the current configuration settings.
/// </summary>
/// <returns>
/// The behavior extension.
/// </returns>
protected override object CreateBehavior()
{
return new EnableCrossOriginResourceSharingBehavior();
}
}
}
/// <summary>
/// Helper method for CORS support.
/// </summary>
[OperationContract]
[WebInvoke(Method = "OPTIONS", UriTemplate = "*")]
void GetOptions();