我創建了一個使用VS 2008的WCF SOAP服務,它用於服務器端。我加入了下面的代碼,使用jQuery/json(無ASP.NET腳本管理器)調用相同的服務/合同客戶端。當我將服務url放入瀏覽器時,我得到正確的服務頁面,當我從JavaScript調用服務並通過Firebug跟蹤時,我得到「405方法不允許」。jQuery Ajax調用WCF服務返回「方法不允許(405)」
ajax()錯誤函數的statusText和responseText不包含任何內容,並且出於某種奇怪的原因錯誤函數被調用兩次。我也從https頁面調用服務。
相同的ajax代碼與服務器端和客戶端的類似傳統web服務一起工作,也可以使用頁面方法。
任何線索?我還注意到我的Windows Communication Foundation HTTP激活和非HTTP激活未安裝。我需要這些嗎?儘管我的服務器端WCF服務工作。
接口:
[ServiceContract]
public interface ICust
{
[OperationContract]
[WebInvoke(Method = "POST",
BodyStyle = WebMessageBodyStyle.Wrapped,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
bool GetCust(string zzz);
}
類別:
public class Cust: ICust
{
public bool GetCust(string zzz)
{
// Do Stuff
// Return true/false;
}
}
的web.config:
<behaviors>
<serviceBehaviors>
<behavior name="E.W.CustomerBehavior" >
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="webCallable">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="E.W.CustomerBehavior"
name="E.W.Customer">
<endpoint address="http://zzzz/Cust.svc"
binding="webHttpBinding"
contract="E.W.ICust"
behaviorConfiguration="webCallable">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
的jQuery:
$.ajax({
cache: false,
async: false,
type: "POST",
url: "http://zzzz/Cust.svc/GetCust",
contentType: "application/json",
dataType: "json",
data: "{'zzz': '" + $("#ctl00_zz_zzz").val() + "'}",
success: function(msg) {
// do stuff
},
error: function(z) { alert(z.responseText); }
});
我有這在web.config中,它沒有工作。我確實把上面的代碼隱藏起來,但沒有運氣。 – Steve 2010-06-16 21:35:53