2011-05-30 63 views
1

我有一個asmx web服務,它返回一個大陸的國家列表。當使用jQuery來調用Web服務使用:將asmx Web服務轉換爲WCF Web服務 - 爲什麼JSON參數需要額外的引號?

$.ajax({ 
      type: "POST", 
      contentType: "application/json; charset=utf-8", 
      url: "InternationalLookup.asmx/LoadCountries", 
      data: '{ continentName: "' + $(this).val() + '" }', 
      dataType: "json", 
      success: function (response) { 
       //..code 
      }, 
      error: function (response) { 
       //..code 
      } 
     }); 

也能正常工作與ASMX代碼,但使用WCF服務時,我不得不將其更改爲:

$.ajax({ 
      type: "POST", 
      contentType: "application/json; charset=utf-8", 
      url: "InternationalLookup.svc/LoadCountries", 
      **data: '{ \"continentName\": "' + $(this).val() + '" }',** 
      dataType: "json", 
      success: function (response) { 
       //..code 
      }, 
      error: function (response) { 
       //..code 
      } 
     }); 

注的區別我必須通過的數據,現在需要在大陸名稱周圍增加引號。我的WCF服務,它的配置:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <system.serviceModel> 
<behaviors> 
    <serviceBehaviors> 
    <behavior name="InternationalLookupBehavior"> 
     <serviceMetadata httpGetEnabled="true" /> 
     <serviceDebug includeExceptionDetailInFaults="true" /> 
    </behavior> 
    </serviceBehaviors> 
    <endpointBehaviors> 
    <behavior name="InternationalLookup"> 
     <enableWebScript /> 
    </behavior> 
    </endpointBehaviors> 
</behaviors> 
<services> 
    <service behaviorConfiguration="InternationalLookupBehavior" 
     name="USQ.Websites.RefreshLayout.Webservices.UsqInternationalLookup"> 
    <endpoint address="" binding="wsHttpBinding" contract="IInternationalLookup"> 
     <identity> 
     <dns value="localhost" /> 
     </identity> 
    </endpoint> 
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
    </service> 
</services> 

[ServiceContract] 
public interface IInternationalLookup 
{ 
    [OperationContract] 
    [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] 
    string LoadCountries(string continentName); 
} 

儘管有相當一些麻煩得到它的工作,我想知道爲什麼在WCF web服務的參數必須被包裹在額外的引號。

回答

0

JSON規範指出對象成員名稱必須用雙引號括起來 - 請參閱www.json.org - 所以這就是WCF執行的內容。我不知道爲什麼ASMX服務使用的JSON解析器選擇在執行語法時更加鬆懈。