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

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

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

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

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

WCF是自托管的,Web应用程序位于IIS 7.5上.

Chrome显示的内容:

小提琴手的节目

合同

Function LookUpPerson(person As Person) _

As List(Of Person)

app.config

crossDomainScriptAccessEnabled="true">

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

}

});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.

总结

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。

小编个人微信号 jb51ccc

喜欢与人分享编程技术与工作经验,欢迎加入编程之家官方交流群!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值