【问题标题】:Converting asmx web-service to WCF web-service - why does the JSON parameter need additional quotation marks?将 asmx web-service 转换为 WCF web-service - 为什么 JSON 参数需要额外的引号?
【发布时间】:2011-05-30 00:54:18
【问题描述】:

我有一个 asmx 网络服务,它返回一个大陆的国家列表。使用 JQuery 调用我使用的网络服务时:

$.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 服务的参数必须用额外的引号括起来。

【问题讨论】:

    标签: wcf web-services json asmx


    【解决方案1】:

    JSON 规范规定对象成员名称必须用双引号括起来 - 请参阅 www.json.org - 所以这是 WCF 强制执行的。我不知道为什么 ASMX 服务使用的 JSON 解析器选择更加宽松地执行语法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-17
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 2014-05-20
      • 2010-11-03
      相关资源
      最近更新 更多