Ajax调用WCF报405错误

环境:jquery1.8.1+Wcf(IIS托管)

错误:405错误(方法不被允许)

原因:ajax跨域调用错误

解决办法:

1.在发布WCF上面允许crossDomainScriptAccessEnabled

<system.serviceModel>
        <behaviors>

            <endpointBehaviors>
                <behavior name="JSONPAspNetAjaxBehavior">
                    <enableWebScript />
                </behavior>
            </endpointBehaviors>

            <serviceBehaviors>
                <behavior name="EnableMetadataBehaviors">
                    <serviceMetadata httpGetEnabled="true" />
                    <serviceDebug includeExceptionDetailInFaults="true" />
                </behavior>
            </serviceBehaviors>
        </behaviors>

        <bindings>
            <webHttpBinding>
        
                <binding name="HttpJsonpBinding" crossDomainScriptAccessEnabled="true"></binding>
            </webHttpBinding>
        </bindings>

        <serviceHostingEnvironment aspNetCompatibilityEnabled="true"    multipleSiteBindingsEnabled="true" >
            <serviceActivations>
                <add relativeAddress="FieldService.svc"   service="FieldService" factory="System.ServiceModel.Activation.WebScriptServiceHostFactory"/>

            </serviceActivations>
        </serviceHostingEnvironment>

        <services>
            <service name="FieldService" behaviorConfiguration="EnableMetadataBehaviors">
                <endpoint address="" behaviorConfiguration="JSONPAspNetAjaxBehavior" binding="webHttpBinding" bindingConfiguration="HttpJsonpBinding" contract="FieldService" />
            </service>
        </services>
    </system.serviceModel>

 

2.实现类加上支持回调

[JavascriptCallbackBehavior(UrlParameterName = "jsoncallback")]

3.操作方法上加特性只支持Get,并且返回的数据类型是JSON

[OperationContract]
    [WebGet(ResponseFormat = WebMessageFormat.Json)]

 

4.客户端JS调用需要加上参数jsoncallback

/DataBaseGetService.svc/GetItemClassSelect?jsoncallback=?

好了,大致4个步骤。我也是从网上找到的解决方案,记录下,送给需要的同学

posted on 2013-05-13 13:55  ~紫鱼~  阅读(3031)  评论(0编辑  收藏  举报