jQuery .ajax()405(不允许的方法)/跨域
我已经看到了很多关于此的问题,但是我找不到代码中缺少的东西。
我正在实现CORS因为我不想使用JSONP
。
我知道这是一个preflighted请求 ,我想我正在添加正确的headers
。
错误是网站似乎不喜欢我的WCF
,每次我做一个请求时,即使我有Access-Control-Allow-Methods
标头,也会调用一个OPTION
方法。
我只想用一个contentType: "application/json",
对我的WCF
进行POST
调用contentType: "application/json",
WCF
是自托管的,Web应用程序位于IIS 7.5
。
Chrome显示的内容:
小提琴手表现出什么
合同
Function LookUpPerson(person As Person) _ As List(Of Person)
的app.config
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
对不起,我希望具体。
提前致谢。
UPDATE
如果我使用contentType: "text/plain"
我在chrome控制台上收到错误:
POST
http://192.168.0.61:8282/Project.Services.Person/LookUpPerson
400(错误请求)
我有同样的问题,这为我解决了。
更改
[WebInvoke(Method = "Post")]
至
[WebInvoke(Method = "*")]
因为虽然您接受POST
,但现代浏览器将始终发送OPTIONS
。