我已经看到了很多关于此问题的问题,但我找不到代码中缺少的内容。
我正在实施CORS因为我不想使用JSONP
。
我知道这是preflighted request,我想我正在添加正确的headers
。
错误是该网站似乎不喜欢我的WCF
,每次我发出请求时,即使我拥有OPTION
标题,也会调用Access-Control-Allow-Methods
方法
我只想通过POST
WCF
进行contentType: "application/json",
来电
WCF
是自托管的,网络应用程序位于IIS 7.5
。
Chrome显示的内容:
小提琴手显示
合同
<OperationContract()>
<WebInvoke(Method:="POST",
RequestFormat:=WebMessageFormat.Json,
ResponseFormat:=WebMessageFormat.Json,
BodyStyle:=WebMessageBodyStyle.WrappedRequest)>
<FaultContract(GetType(ServiceFault))>
Function LookUpPerson(person As Person) _
As List(Of Person)
的app.config
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
<bindings>
<webHttpBinding>
<binding name="webHttpBindingWithJsonP"
crossDomainScriptAccessEnabled="true">
<security mode="None" />
</binding>
</webHttpBinding>
</bindings>
<services>
<service name="Project.Services.Person">
<endpoint address="ws" binding="wsHttpBinding" contract="Project.Services.Interfaces.IPublic" />
<endpoint address="" binding="webHttpBinding" contract="Project.Services.Interfaces.IPublic"
behaviorConfiguration="jsonBehavior"/>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8732/" />
</baseAddresses>
</host>
</service>
</services>
<extensions>
<behaviorExtensions>
<add name="customHeaders"
type="Project.Services.Utilities.EnableCrossOriginResourceSharingBehavior, Project.Services, Version=1.0.0.0, Culture=neutral"/>
</behaviorExtensions>
</extensions>
<endpointBehaviors>
<behavior name="jsonBehavior">
<webHttp/>
<customHeaders />
</behavior>
</endpointBehaviors>
的javascript
$.ajax({
url: "http://192.168.0.61:8282/Project.Services.Person/LookUpPerson",
type: "POST",
contentType: "application/json",
crossDomain: true,
dataType: "json",
data: { person: JSON.stringify(person) },
success: function (data) {
// doing something
},
error: function (error) {
// doing something
}
});
根据http://enable-cors.org/server_wcf.html
,在WCF上我有以下处理程序Public Class CustomHeaderMessageInspector
Implements IDispatchMessageInspector
Private requiredHeaders As Dictionary(Of String, String)
Public Sub New(headers As Dictionary(Of String, String))
requiredHeaders = If(headers, New Dictionary(Of String, String)())
End Sub
Public Function AfterReceiveRequest(ByRef request As System.ServiceModel.Channels.Message,
channel As System.ServiceModel.IClientChannel,
instanceContext As System.ServiceModel.InstanceContext) _
As Object _
Implements System.ServiceModel.Dispatcher.IDispatchMessageInspector.AfterReceiveRequest
Return Nothing
End Function
Public Sub BeforeSendReply(ByRef reply As System.ServiceModel.Channels.Message,
correlationState As Object) _
Implements System.ServiceModel.Dispatcher.IDispatchMessageInspector.BeforeSendReply
Dim httpHeader = TryCast(reply.Properties("httpResponse"), HttpResponseMessageProperty)
For Each item In requiredHeaders
httpHeader.Headers.Add(item.Key, item.Value)
Next
End Sub
End Class
和
Public Class EnableCrossOriginResourceSharingBehavior
Inherits BehaviorExtensionElement
Implements IEndpointBehavior
Public Sub AddBindingParameters(endpoint As ServiceEndpoint, bindingParameters As System.ServiceModel.Channels.BindingParameterCollection) _
Implements System.ServiceModel.Description.IEndpointBehavior.AddBindingParameters
End Sub
Public Sub ApplyClientBehavior(endpoint As ServiceEndpoint, clientRuntime As System.ServiceModel.Dispatcher.ClientRuntime) _
Implements System.ServiceModel.Description.IEndpointBehavior.ApplyClientBehavior
End Sub
Public Sub ApplyDispatchBehavior(endpoint As ServiceEndpoint, endpointDispatcher As System.ServiceModel.Dispatcher.EndpointDispatcher) _
Implements System.ServiceModel.Description.IEndpointBehavior.ApplyDispatchBehavior
Dim requiredHeaders = New Dictionary(Of String, String)()
requiredHeaders.Add("Access-Control-Allow-Origin", "*")
requiredHeaders.Add("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
requiredHeaders.Add("Access-Control-Allow-Headers", "Origin, Content-Type, Accept")
requiredHeaders.Add("Access-Control-Max-Age", "1728000")
endpointDispatcher.DispatchRuntime.MessageInspectors.Add(New CustomHeaderMessageInspector(requiredHeaders))
End Sub
Public Sub Validate(endpoint As ServiceEndpoint) _
Implements System.ServiceModel.Description.IEndpointBehavior.Validate
End Sub
Public Overrides ReadOnly Property BehaviorType() As Type
Get
Return GetType(EnableCrossOriginResourceSharingBehavior)
End Get
End Property
Protected Overrides Function CreateBehavior() As Object
Return New EnableCrossOriginResourceSharingBehavior()
End Function
End Class
对于这篇长篇文章感到抱歉,我想具体一点。
提前致谢。
更新
如果我使用contentType: "text/plain"
,我会在Chrome控制台上收到错误:
POST
http://192.168.0.61:8282/Project.Services.Person/LookUpPerson
400(错误请求)
答案 0 :(得分:10)
我遇到了同样的问题,并为我解决了这个问题。
更改
[WebInvoke(Method = "Post")]
到
[WebInvoke(Method = "*")]
因为虽然您接受POST
,但现代浏览器始终会发送OPTIONS
。