WCF JSON服务有两个通过jquery调用的方法。 GET方法有效,POST方法返回404错误

时间:2012-11-24 18:39:55

标签: c# jquery json wcf

我有一个简单的WCF服务,其中包含以下界面:

[ServiceContract]
public interface IPageService {

    [OperationContract]
    [WebGet(UriTemplate = "/GetPage/{pageNumber}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    Page GetPage(string pageNumber);

    [OperationContract]
    [WebInvoke(UriTemplate = "/SetPages", Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    string SetPages(Page[] pages);
}

配置文件的system.serviceModel部分如下:

<system.serviceModel>
  <protocolMapping>
    <add scheme="http" binding="webHttpBinding"/>
  </protocolMapping>
  <behaviors>
    <serviceBehaviors>
      <behavior>
        <serviceMetadata httpGetEnabled="true"/>
        <serviceDebug includeExceptionDetailInFaults="true"/>
      </behavior>
    </serviceBehaviors>
    <endpointBehaviors>
      <behavior>
        <webHttp/>
      </behavior>
    </endpointBehaviors>
  </behaviors>
  <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>

使用以下JavaScript调用GetPage方法:

$.ajax({
    cache: false,
    url: 'http://localhost/Test/PageService.svc/GetPage/1', 
    type: 'GET',
    success: function(result) {
        // do success stuff 
    }, 
    error: function(req, status, error) { 
        // do error stuff
    } 
});

使用以下JavaScript调用SetPages方法会返回404错误:

$.ajax({
    cache: false,
    url: 'http://localhost/Test/PageService.svc/SavePages', 
    type: 'POST',
    data: '[{...}]', 
    dateType: 'json',
    contentType: 'application/json',
    processData: false,
    success: function(result) { 
        // do success stuff 
    }, 
    error: function(req, status, error) { 
        // do error stuff 
    } 
});

我已经在ajax调用中尝试了几乎所有参数组合,没有任何区别。我已经玩过配置文件并尝试了各种配置和各种博客,但所有这些都使得两种方法都返回AddressFilter或ContractFilter不匹配。我错过了什么?让这两种方法都起作用的最快捷/最简单的方法是什么?

1 个答案:

答案 0 :(得分:2)

根据您发布的代码,jscript调用SavePages方法,但在服务器端,post方法调用名称SetPages。