将 asmx Web 服务转换为 WCF Web 服务 - 为什么 JSON 参数需要额外的引号?
我有一个 asmx Web 服务,它返回一个大陆的国家/地区列表。当使用 JQuery 调用 Web 服务时,我使用:
$.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 服务的参数必须用额外的引号括起来。
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.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);
}
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
JSON 规范规定对象成员名称必须用双引号括起来 - 请参阅 www.json.org - 所以这是 WCF 强制执行的。我不知道为什么 ASMX 服务使用的 JSON 解析器在执行语法时选择更加宽松。
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.