ajax – MVC 5无效的JSON原语

前端之家收集整理的这篇文章主要介绍了ajax – MVC 5无效的JSON原语前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图在MVC5中用ajax编写自动完成,但无论我尝试什么,我都会得到错误无效的 JSON原语.
当我手动输入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);
    };

});
评论有点长,所以加上这个答案 –

首先要尝试修改每篇帖子的内容类型:
MVC JSON method returning invalid JSON to JQuery?

试试“application / json”.

还包装你的数据对象

JSON.stringify()

如果这不起作用,你可以告诉我你的行动结果来自哪里 – 我没有看到声明.还要添加断点并确保您可以进入该方法.

原文链接:https://www.f2er.com/ajax/159893.html

猜你在找的Ajax相关文章