• 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个步骤。我也是从网上找到的解决方案,记录下,送给需要的同学

  • 相关阅读:
    hdu 1077计算几何
    hdu 1110几何题
    hdu 4430二分枚举
    numpy常用技巧
    python中数组(list/array)不会复制,而是直接引用
    怎么在ASP.NET 2.0中使用Membership
    2分法通用存储过程分页(top max模式)版本(性能相对之前的not in版本极大提高)
    Oracle大数据量分页通用存储过程
    JavaScript 对象与数组参考大全
    ajax框架比较
  • 原文地址:https://www.cnblogs.com/flyfish2012/p/3075631.html
Copyright © 2020-2023  润新知