帮助jquery自动完成和json响应

时间:2010-03-05 18:59:10

标签: jquery autocomplete jquery-autocomplete

我有一个ASP.NET 2.0 json Web服务,它返回以下响应

<?xml version="1.0" encoding="utf-8" ?> 
  <string xmlns="http://microsoft.com/webservices/">[{"CUName":"Raytown-Lee\u0027s Summit Comm CU","CUCity":"RAYTOWN","CUState":"MO","CUContractNo":"02406"},{"CUName":"Summit Credit Union","CUCity":"MADISON","CUState":"WI","CUContractNo":"04800"},{"CUName":"Summit Credit Union","CUCity":"GREENSBORO","CUState":"NC","CUContractNo":"03200"},{"CUName":"Summit Hampton Roads FCU","CUCity":"NORFOLK","CUState":"VA","CUContractNo":"04504"},{"CUName":"SummitOne Federal CU","CUCity":"OGDEN","CUState":"UT","CUContractNo":"14301"}]</string>

当我将此绑定到我的测试框以与自动完成插件一起使用时,我在下拉列表中看不到任何结果。我用萤火虫检查了电话的确定。

我的前端呼叫如下所示

$(document).ready(function() {
 $("#city").autocomplete("CUList.asmx/GetCUList", {
  dataType: 'jsonp',
  parse: function(data) 
  {
   var rows = new Array();
   for(var i=0; i<data.length; i++){
    rows[i] = { data:data[i], value:data[i].CUName, result:data[i].CUName };
   }
   return rows;
  },
  formatItem: function(row, i, n) {
   return row.CUName + ', ' + row.CUCity;
  },
  max: 50
 }); 
  });

有人可以让我知道我做错了吗?

由于

1 个答案:

答案 0 :(得分:0)

那不是JSON :)这是一个用XML包装的JSON字符串。你需要让你的WebMethod返回JSON而不是XML。

例如装饰你的WebMethod:

[WebMethod, ScriptMethod]
public List<thing> GetCUList()