javascript – jQuery .ajax()405(不允许的方法)/跨域

前端之家收集整理的这篇文章主要介绍了javascript – jQuery .ajax()405(不允许的方法)/跨域前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我已经看到了很多关于此的问题,但是我找不到代码中缺少的东西.

我正在实现CORS因为我不想使用JSONP.

我知道这是preflighted request,我想我正在添加正确的标题.

错误是网站似乎不喜欢我的WCF,每次我做一个请求时,即使我有Access-Control-Allow-Methods标头,也会调用一个OPTION方法.

我只想用一个contentType:“application / json”对我的WCF进行POST调用,

WCF是自托管的,Web应用程序位于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" />

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 (Bad Request)

最佳答案

我有同样的问题,这为我解决了.

更改

[WebInvoke(Method = "Post")]

[WebInvoke(Method = "*")]

因为虽然您接受POST,但现代浏览器将始终发送OPTIONS.

原文链接:https://www.f2er.com/jquery/428753.html

猜你在找的jQuery相关文章