我创建了一个使用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