gpt4 book ai didi

ajax - MVC 5无效的JSON原语

转载 作者:行者123 更新时间:2023-12-04 10:00:27 24 4
gpt4 key购买 nike

我正在尝试在MVC5中使用Ajax编写自动完成功能,但是无论如何我都会收到错误无效的JSON原语。
当我手动输入url时,localhost:5088/GetData?inpt = [query]我可以看到返回的json。

根据我在网上的了解,我给“data:”参数错误。我尝试将它们放在“”中,但没有用。

我的 Controller :

  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()

如果那不起作用,您能告诉我您的操作结果来自何处-我看不到声明。还添加一个断点,并确保您可以进入该方法。

关于ajax - MVC 5无效的JSON原语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22361067/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com