使用ASP MVC进行jQuery UI自动完成

我正在尝试让jQuery Automcomplete工作,但它不会按我的意愿执行:P这是我的代码:

JavaScript的:

$("#CustomerID").autocomplete({ source: function(request, response) { $.ajax({ type: "POST", url: "/customer/search", dataType: "json", data: { term: request.term }, error: function(xhr, textStatus, errorThrown) { alert('Error: ' + xhr.responseText); }, success: function(data) { response($.map(data, function(c) { return { label: c.Company, value: c.ID } })); } }); }, minLength: 2, select: function(event, ui) { alert('Select'); } }); 

ASP MVC:

  [AcceptVerbs(HttpVerbs.Post)] public JsonResult Search(string term) { if (term == null) term = ""; List customers = repCustomer.FindCustomers(term).ToList(); return Json(customers); } public class JSON_Customer { public int ID { get; set; } public string Company { get; set; } } public IQueryable FindCustomers(string searchText) { return from c in _db.Customers where c.Company.Contains(searchText) orderby c.Company select new JSON_Customer { ID = c.ID, Company = c.Company }; } 

我从$.ajax收到请求,并根据搜索字词返回正确的客户列表。 并调用success方法。 我可以看到data[object Object]值,但接下来我该怎么做? 没有客户在我的列表中掉线。 我正在使用response($.map...来自http://jqueryui.com/demos/autocomplete/#remote-jsonp的代码,但它不会工作。

谁知道为什么?

我在第一次AJAX请求之前使用它 – 我打赌它会有所帮助。 定义标准项并处理微软放入的“d”属性作为顶级属性。

  $.ajaxSetup({ type: "POST", contentType: "application/json; charset=utf-8", data: "{}", dataFilter: function(data) { var msg; if (typeof (JSON) !== 'undefined' && typeof (JSON.parse) === 'function') msg = JSON.parse(data); else msg = eval('(' + data + ')'); if (msg.hasOwnProperty('d')) return msg.d; else return msg; } });