将 asmx Web 服务转换为 WCF Web 服务 - 为什么 JSON 参数需要额外的引号?

发布于 2024-11-10 02:56:43 字数 2471 浏览 3 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清音悠歌 2024-11-17 02:56:44

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文