2

我正在尝试将Ajax Auto Complete Extender与托管在 Web 项目中的 WCF 服务一起使用。服务已到达,我已验证结果是使用提琴手返回的,但是与自动完成扩展器关联的文本框永远不会填充。

服务合同如下:

[ScriptService]
[ServiceContract(Namespace = "")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public interface ICertificateService
{
    [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod]
    [WebInvoke(Method = "POST",
        BodyStyle = WebMessageBodyStyle.Wrapped,
        ResponseFormat = WebMessageFormat.Json)]
    List<string> GetCompletionList(string prefixText, int count);
}

该实现只是返回一个填充的字符串列表。

aspx如下:

<asp:TextBox runat="server" ID="aceInstructors"></asp:TextBox>
<ajaxToolkit:AutoCompleteExtender runat="server" 
                                  ID="autoCompleteInstructor"
                                  TargetControlID="aceInstructors"
                                  ServiceMethod="GetCompletionList" 
                                  ServicePath="../../CertificateService" 
                                  MinimumPrefixLength="1"
                                  CompletionInterval="1000" 
                                  EnableCaching="true" CompletionSetCount="5">
                        <Animations>
                            <OnShow>  <HideAction Visible="true" /> </OnShow>
                            <OnHide> <HideAction Visible="false" /> </OnHide>       
                        </Animations>

服务的路由在 Global.asax 中配置如下:

private void RegisterRoutes()
    {
        RouteTable.Routes.Add(new ServiceRoute("CertificateService", new WebServiceHostFactory(), typeof(CertificateService)));
    }

如前所述,我可以热服务,当我在提琴手中观看时,我会收到 JSON 格式的响应。以下是原始响应:

HTTP/1.1 200 OK
Server: Cassini/4.0.1.7
Date: Mon, 12 Sep 2011 16:44:16 GMT
X-AspNet-Version: 4.0.30319
Content-Length: 68
Cache-Control: private
Content-Type: application/json; charset=utf-8
Connection: Close

{"GetCompletionListResult":["Alpha","Beta","Gamma","Delta","Omega"]}

可能值得注意的是,如果我从服务合同中省略了 ResponseFormat,结果将以 XML 格式返回,并且文本框会填充一个很长的未定义列表

我错过了一些基本的东西吗?

4

1 回答 1

2

问题在这里得到解决。我需要解决的问题似乎围绕着 JSON 的包装和返回方式。似乎 ajax 工具包自动完成扩展器期望 JSON 内容是“ .d Wrapped ”。这是通过遵循提供的链接中的配置设置来完成的。

还应该注意的是,有一个支持 Ajax 的 WCF 项目模板添加了这些 web.config 设置......知道这可能会节省一些时间。

于 2011-09-13T13:51:24.267 回答