【问题标题】:REST Service exposed on WCF not populating parameter valuesWCF 上公开的 REST 服务未填充参数值
【发布时间】:2009-01-22 00:40:13
【问题描述】:

我有一个 WCF 服务,它将字符串值“名称”作为参数。这个服务接口长这样……

[ServiceContract(Name = "ContactLookup", Namespace = "Search")]
public interface IAjaxResultSearcherService
{
    [OperationContract]
    [WebGet(RequestFormat = WebMessageFormat.Json)]
    Result[] SearchResultsWithNameLike(string name);
}

使用以下配置公开服务...

<system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
    <behaviors>
        <serviceBehaviors>
            <behavior name="AjaxServiceBehavior">
                <serviceDebug
                       includeExceptionDetailInFaults="true" 
                       httpHelpPageEnabled="true"/>
                <serviceMetadata httpGetEnabled="true" httpGetUrl=""/>
                <serviceCredentials>
                    <serviceCertificate findValue="localhost" 
                                 storeLocation="LocalMachine" 
                                 storeName="My" x509FindType="FindBySubjectName"/>
                </serviceCredentials>
            </behavior>
        </serviceBehaviors>
        <endpointBehaviors>
            <behavior name="JsonBehavior">
                <enableWebScript/>
            </behavior>
        </endpointBehaviors>
    </behaviors>
    <services>
        <service name="HelloIndigo.AjaxResultSearcherService" 
                             behaviorConfiguration="AjaxServiceBehavior">
            <endpoint address="json"
                      binding="webHttpBinding"
                  behaviorConfiguration="JsonBehavior"
                      contract="Contract.IAjaxResultSearcherService" />
        </service>
    </services>
</system.serviceModel>

我的 .svc 文件使用 WebScriptServiceHostFactory。这似乎有效,这意味着我可以从我的 jquery 客户端连接到服务,但是即使我通过 ajax 调用设置了参数值,当消息到达服务实现时,参数值为 null。

我像这样通过 jquery 调用服务...

$.ajax({
    url: url,
    data: { "name":"test" },
    type: "GET",
    processData: false,
    contentType: "application/json",
    timeout: 10000,
    dataType: "text",
    success: function(res) {
        alert('function successful! returned: ' + res.toString());
    },
    error: function(xhr) {
        alert('an error occurred: ' + xhr.responseText);
    }
});

我没有从服务调用中得到任何错误,但是当我调试进程时,参数值为 null。为什么 WCF 不会像我预期的那样传递参数值?

感谢您的帮助。

【问题讨论】:

    标签: jquery ajax rest wcf


    【解决方案1】:

    在我的试验中,我尝试使用 JSON.org 的 JSON2 来“字符串化”我的 json 对象并将其作为数据对象传递给 ajax 调用,并将 processData 参数设置为 false。一旦我决定将数据元素保留为本机 JSON 对象并将 processData 设置为 true,我的参数值就会被填充。

    我仍然不确定为什么 JSON2.js 的 stringify 没有使 data 参数有效,但至少这是有效的。

    所以,以前我有以下内容:

    var json = JSON.stringify({ "name":"test" });
    $.ajax({
        url: url,
        data: json,
        type: "GET",
        processData: false,
        contentType: "application/json",
        timeout: 10000,
        dataType: "text",
        success: function(res) {
            alert('function successful! returned: ' + res.toString());
        },
        error: function(xhr) {
            alert('an error occurred: ' + xhr.responseText);
        }
    });
    

    工作版本,但是看起来像这样......

    var json = { "name":"test" };
    $.ajax({
        url: url,
        data: json,
        type: "GET",
        processData: true,
        contentType: "application/json",
        timeout: 10000,
        dataType: "text",
        success: function(res) {
            alert('function successful! returned: ' + res.toString());
        },
        error: function(xhr) {
            alert('an error occurred: ' + xhr.responseText);
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2011-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-09
      • 2012-10-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多