这是我负责填写自动填充的Web服务:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {
[WebMethod]
public List<Farms> GetFarms1()
{
PlaseTableAdapter pta = new PlaseTableAdapter();
PlaseLande.PlaseDataTable pdt = pta.GetData();
List<Farms> farms = new List<Farms>();
foreach (DataRow dr in pdt)
{
string farmName = (string)dr["FARMNAME"];
string farmID = (string)dr["FARMID"];
farms.Add(new Farms(farmID, farmName));
}
return farms;
}
public class Farms
{
public Farms(string farmid, string farmname)
{
FARMID = farmid;
FARMNAME = farmname;
}
public string FARMID { get; set; }
public string FARMNAME { get; set; }
}
}
这是从webservice获取json数据的jquery
$("#txtFarms").autocomplete("WebService.asmx/GetFarms1", {
dataType: 'json',
parse: function(data) {
var parsed = [];
data = data.Farms;
for (var i = 0; i < data.length; i++) {
parsed[parsed.length] = {
data: data[i],
value: data[i].FARMNAME,
result: data[i].FARMNAME
};
}
return parsed;
},
formatItem: function(item) {
alert(item);
return item.FARMNAME + ' (' + item.FARMID + ')';
}
});
我的代码不适用于我缺少的内容? 坦克提供任何帮助!
答案 0 :(得分:0)
寻找真正的json回归。我认为你应该添加一个dataAnnotation ..
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
所以在你的代码中:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public List<Farms> GetFarms1() // why you don't have no parameter like the term of search ?
{
PlaseTableAdapter pta = new PlaseTableAdapter();
PlaseLande.PlaseDataTable pdt = pta.GetData();
List<Farms> farms = new List<Farms>();
foreach (DataRow dr in pdt)
{
string farmName = (string)dr["FARMNAME"];
string farmID = (string)dr["FARMID"];
farms.Add(new Farms(farmID, farmName));
}
return farms;
}
请参阅How to return JSON from webservice
$( "#txtFarms" ).autocomplete({
source: function( request, response ) {
$.ajax({
url: "WebService.asmx/GetFarms1",
dataType: "json",
data: {
name_startsWith: request.term // i don't see it in your code ... why?
},
success: function( data ) {
response( $.map( data.Farms, function( item ) {
return {
label: item.FARMNAME + ' (' + item.FARMID + ')';,
value: item.FARMNAME
}
}));
}
});
更多信息:http://jqueryui.com/autocomplete/#remote-jsonp
它对你有帮助吗?