首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将asmx web-service转换为WCF web-service -为什么JSON参数需要额外的引号?

将asmx web-service转换为WCF web-service -为什么JSON参数需要额外的引号?
EN

Stack Overflow用户
提问于 2011-05-30 08:54:19
回答 1查看 676关注 0票数 1

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

代码语言:javascript
复制
$.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服务时,我必须将其更改为:

代码语言:javascript
复制
$.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服务及其配置:

代码语言:javascript
复制
<?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>
代码语言:javascript
复制
[ServiceContract]
public interface IInternationalLookup
{
    [OperationContract]
    [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    string LoadCountries(string continentName);
}

尽管有相当多的麻烦让它工作,我想知道为什么WCF web服务的参数必须用额外的引号括起来。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-05-30 09:53:08

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6171421

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档