405 WCF中的方法不允许错误

时间:2010-03-12 21:57:46

标签: asp.net wcf

有人能发现这个实现的问题吗?我可以在浏览器中打开它并且它可以工作,但是来自客户端的调用(使用jquery和asp.net ajax都失败了)

服务合同

[OperationContract(Name = "GetTestString")]
[WebInvoke(Method = "GET",
           ResponseFormat = WebMessageFormat.Json
   )]
string GetTestString();

在Web.config和其他绑定之间,我有一个webHttp绑定

<endpoint address="ajax" binding="webHttpBinding" contract="TestService" behaviorConfiguration="AjaxBehavior" />

终点行为

  <endpointBehaviors>
    <behavior name="AjaxBehavior">
      <enableWebScript/>
    </behavior>
  </endpointBehaviors>
</behaviors>

Svc文件

<%@ ServiceHost Service="TestService" %>

客户端

var serviceUrl = "http://127.0.0.1/Test.svc/ajax/";
var proxy = new ServiceProxy(serviceUrl);

然后我在http://www.west-wind.com/weblog/posts/324917.aspx中使用该方法 致电服务

2 个答案:

答案 0 :(得分:6)

链接上的示例使用Http POST,而不是Http GET。这是“不允许的方法” - 你需要改变代码来代替GET。

您发布的链接是您的客户端代码来源:

 $.ajax( { 
                url: url,
                data: json,
                type: "POST",
                processData: false,
                contentType: "application/json",
                timeout: 10000,
                dataType: "text",  // not "json" we'll parse

注意那里的type: "POST" - 你的需要是“GET”。我假设你从你发布的链接中获取了你的JQuery,因为405状态表明你的调用代码是错误的,而不是服务。

答案 1 :(得分:1)

对于方法不允许错误,您需要检查的是确保您的http Web调用/请求与服务中[WebInvoke ...]中指定的相同

  $.ajax({
                type: "POST",.....});

应该在服务接口中指明的是什么(在“[运营合同]”下)

 [WebInvoke(Method = "POST",BodyStyle = WebMessageBodyStyle.Wrapped,ResponseFormat = WebMessageFormat.Json)]
相关问题