我试图在MVC5中用ajax编写自动完成,但无论我尝试什么,我都会得到错误无效的
JSON原语.
当我手动输入url localhost:5088 / GetData?inpt = [query]我能看到返回json.
原文链接:https://www.f2er.com/ajax/159893.html当我手动输入url localhost:5088 / GetData?inpt = [query]我能看到返回json.
根据我的在线理解,我给出了“data:”参数错误.我尝试将它们放入“”但它没有用.
我的控制器:
public JsonResult GetData(string inpt) { try { var node = //some values here,cause its too long I deleted it foreach (var node in q) { string scity = node.Attribute("CityName").Value.ToUpper(new CultureInfo("tr-TR",false)); string ccity = node.Attribute("CityCode").Value; string ccode = node.Attribute("CountryCode").Value; if (ccity != oldcity) { result.Add(new HavaAlani { SehirAdi = scity,HavaAlaniKodu = ccity,HavaAlaniAdi = scity + "," + ccode,Sehirmi = true }); oldcity = ccity; } result.Add(new HavaAlani { SehirAdi = scity,HavaAlaniKodu = node.Attribute("Code").Value,HavaAlaniAdi = node.Value,Sehirmi = false }); } } catch { } return Json(result,JsonRequestBehavior.AllowGet); } }
我的JS:
$('input.suggestBox').each(function () { //$(this).jsonSuggest(air.Lines); $(this).autocomplete({ source: function (request,response) { $.ajax({ url: "dataAl/GetData",data: { inpt: request.term },dataType: "json",type: "POST",contentType: "application/json; charset=utf-8",dataFilter: function (data) { return data; },success: function (data) { response($.map(data.d,function (item) { return { code: item.HavaAlaniKodu,value: item.HavaAlaniAdi,iscity: item.Sehirmi } })) },error: function (XMLHttpRequest,textStatus,errorThrown) { alert(XMLHttpRequest.responseText); } }); },minLength: 2 }).data("ui-autocomplete")._renderItem = function (ul,item) { var cls = "airport"; if (item.iscity) cls = "city"; return $("<li></li>") .data("item.autocomplete",item) .append("<span class='span-" + cls + "'></span>") .append("<a class='ui-autocomplete-" + cls + "'>" + item.value + " (" + item.code + ")" + "</a>") .appendTo(ul); }; });