【发布时间】:2011-03-04 16:22:20
【问题描述】:
我使用 VS 2008 创建了一个在服务器端工作的 WCF SOAP 服务。我添加了下面的代码,使用 jQuery/json(没有 ASP.NET 脚本管理器)调用相同的服务/合同客户端。当我将服务 url 放入浏览器时,我得到了正确的服务页面,当我从 javascript 调用服务并通过 Firebug 跟踪时,我得到“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); }
});
【问题讨论】: