简体   繁体   中英

Help with jquery autocomplete and json response

I have the a ASP.NET 2.0 json web service that returns the following response

<?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>

When I bind this to my test box for use with the autocomplete plugin, I don't see any results in the dropdown. I checked with firebug that the call is made.

My front end call looks like below

$(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
 }); 
  });

Can someone please let me know what I am doing wrong?

Thanks

That's not JSON :) That's a JSON string wrapped in XML. Yo need to make your WebMethod return JSON instead of XML.

For example decorate your WebMethod:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM