Jquery:ajax调用工作和自动完成不与WCF一起工作

我正在使用jquery来弄清楚如何使用autocomplete

我有两个输入类型文本exampleexample2 。 我只是想知道为什么它适用于ajax调用示例文本框而不适用于example2文本框上的自动完成方法。

这是WCF服务接口:

 [ServiceContract] public interface IMyService { [WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "getcompletions/q={q}&limit={limit}")] List GetCompletions(string q, string limit); } 

当我使用ajax调用它很好地工作:

 $(document).ready(function () { var url = "http://localhost.:62138/"; var data = null; /* //this webservice call works for UriTemplate = "getcompletions/{q}" $.ajax({ url: url + "MyService.svc/GetCompletions/" + $('#example').val(), cache: false, type: "GET", // http method dataType: "json", error: function (XMLHttpRequest, status, error) { //alert("Error p1 s(" + status + ") e(" + error + ")"); }, success: function (msg, arg2, xhr) { try { if (msg !== null) {data = msg;} else { alert("msg is null"); } } catch (e) { alert("exception: " + e); }} }); */ //but this doesn't work $('#example2').autocomplete(url + "MyService.svc/GetCompletions/"); }); 

以下是fiddler中autocomplete的请求:

 GET http://localhost.:62138/MyService.svc/GetCompletions/?q=apri&limit=150&timestamp=1289668676175 HTTP/1.1 Accept: */* Accept-Language: en-US User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; .NET4.0E; .NET4.0C) Accept-Encoding: gzip, deflate Connection: Keep-Alive Host: localhost.:62138 

以下是fiddler失败时autocomplete的结果:

 HTTP/1.1 404 Not Found Server: ASP.NET Development Server/10.0.0.0 Date: Sat, 13 Nov 2010 17:19:53 GMT X-AspNet-Version: 4.0.30319 Content-Length: 1565 Cache-Control: private Content-Type: text/html; charset=UTF-8 Connection: Close     Service blabla....   

Service

Endpoint not found.

这是web.config

                   <!--post -->                   

现在我得到了这个带有自动完成function的example2文本框。 在这里我修复了一些事情:

在javascript中,删除尾部斜杠(/):

 $('#example2').autocomplete(url + "ChatService.svc/GetCompletions"); 

web.config,替换为没有wierd tweek的新的:

                     

接口:

 [ServiceContract(Namespace = "YourCompany.Services")] public interface IMyService { [OperationContract] [WebInvoke(Method = "GET", UriTemplate = "GetCompletions?q={q}", ResponseFormat = WebMessageFormat.Json)] string GetCompletions(string q); } 

类方法GetCompletions一个非常重要的tweek, GetCompletions遵循自动完成插件的预期格式:

 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class MyService : IMyService { public string GetCompletions(string q) { List words = new List { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December", "Yammer", "Yaw", "Yawn", "Auspiscious", "Arbitrage", "Arbiter", "Arbor", "Ardor", "Ardent", "Concrete", "Conscious", "Uptight", "Uplevel", "Friend", "Depend", "Deepend", "Deepen", "Decommit", "Right", "Now", "Knowledge", "Knight", "Know", "Knickers", "Wow", "Holy",}; var selection = from candidate in words where candidate.ToUpper().StartsWith(q.ToUpper()) select candidate; //autocomplete funny format return string.Join("\n", selection.ToArray()); } }