環境: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個步驟。我也是從網上找到的解決方案,記錄下,送給需要的同學
