Converting asmx web-service to WCF web-service - why does the JSON parameter need additional quotation marks?
I have a asmx web-service that returns a list of countries for a continent. When using JQuery to call the web-service I use:
$.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
}
});
This works fine with the asmx code but when using the WCF service I have to change it to:
$.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
}
});
Note the difference in the data I have to pass in, it now requires additional quotation marks around the continent name. My WCF service and it's configuration:
<?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.Websi开发者_StackOverflow社区tes.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);
}
Despite having quite some troubles getting it to work I would like to know why the parameter for the WCF web-service has to be wrapped in additional quotation marks.
The JSON specification states that object member names must be enclosed by double quotes - see www.json.org - so this is what WCF enforces. I don't know why the JSON parser used by ASMX services chose to be more lax in enforcing the syntax.
精彩评论