2009-01-22 113 views
1

我有一個WCF服務,它將字符串值「name」作爲參數。這種服務接口看起來像這樣...在WCF上暴露的REST服務沒有填充參數值

[ServiceContract(Name = "ContactLookup", Namespace = "Search")] 
public interface IAjaxResultSearcherService 
{ 
    [OperationContract] 
    [WebGet(RequestFormat = WebMessageFormat.Json)] 
    Result[] SearchResultsWithNameLike(string name); 
} 

服務暴露在使用以下配置...

<system.serviceModel> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> 
    <behaviors> 
     <serviceBehaviors> 
      <behavior name="AjaxServiceBehavior"> 
       <serviceDebug 
         includeExceptionDetailInFaults="true" 
         httpHelpPageEnabled="true"/> 
       <serviceMetadata httpGetEnabled="true" httpGetUrl=""/> 
       <serviceCredentials> 
        <serviceCertificate findValue="localhost" 
           storeLocation="LocalMachine" 
           storeName="My" x509FindType="FindBySubjectName"/> 
       </serviceCredentials> 
      </behavior> 
     </serviceBehaviors> 
     <endpointBehaviors> 
      <behavior name="JsonBehavior"> 
       <enableWebScript/> 
      </behavior> 
     </endpointBehaviors> 
    </behaviors> 
    <services> 
     <service name="HelloIndigo.AjaxResultSearcherService" 
          behaviorConfiguration="AjaxServiceBehavior"> 
      <endpoint address="json" 
         binding="webHttpBinding" 
        behaviorConfiguration="JsonBehavior" 
         contract="Contract.IAjaxResultSearcherService" /> 
     </service> 
    </services> 
</system.serviceModel> 

我的.svc文件使用WebScriptServiceHostFactory。這似乎工作,這意味着我可以連接到我的jquery客戶端的服務,但即使通過ajax調用設置參數值,當消息到達服務實現時,參數值爲null。

我通過jQuery調用服務是這樣的...

$.ajax({ 
    url: url, 
    data: { "name":"test" }, 
    type: "GET", 
    processData: false, 
    contentType: "application/json", 
    timeout: 10000, 
    dataType: "text", 
    success: function(res) { 
     alert('function successful! returned: ' + res.toString()); 
    }, 
    error: function(xhr) { 
     alert('an error occurred: ' + xhr.responseText); 
    } 
}); 

我沒有得到任何來自服務調用的錯誤,但是當我調試過程中,參數值爲null。爲什麼WCF不會像我期望的那樣傳遞參數值?

感謝您的幫助。

回答

1

在我的考驗,我試圖用JSON.org的JSON2到「字符串」我的JSON對象並將其傳遞給Ajax調用的數據對象,並已設置過程數據參數設置爲false。一旦我決定將數據元素作爲本機JSON對象並將processData設置爲true,我的參數值就被填充了。

我仍然不確定爲什麼JSON2.js的stringify沒有使數據參數有效,但至少這是工作。

所以,以前我有以下幾點:

var json = JSON.stringify({ "name":"test" }); 
$.ajax({ 
    url: url, 
    data: json, 
    type: "GET", 
    processData: false, 
    contentType: "application/json", 
    timeout: 10000, 
    dataType: "text", 
    success: function(res) { 
     alert('function successful! returned: ' + res.toString()); 
    }, 
    error: function(xhr) { 
     alert('an error occurred: ' + xhr.responseText); 
    } 
}); 

工作版本,不過看起來像這樣...

var json = { "name":"test" }; 
$.ajax({ 
    url: url, 
    data: json, 
    type: "GET", 
    processData: true, 
    contentType: "application/json", 
    timeout: 10000, 
    dataType: "text", 
    success: function(res) { 
     alert('function successful! returned: ' + res.toString()); 
    }, 
    error: function(xhr) { 
     alert('an error occurred: ' + xhr.responseText); 
    } 
});