gpt4 book ai didi

c# - Json 无法解析 ajax post 请求

转载 作者:行者123 更新时间:2023-11-30 22:54:07 24 4
gpt4 key购买 nike

我有一个 ajax 请求,从输入字段获取字符串并将其发送到服务器,期望接收对象集合。我应该收到一组笔记,其中笔记有对象日志、对象类别、对象用户,但是,如果我删除它们而不包括它们,解析工作正常。

获取集合的方式:

public ICollection<NoteViewModel> SearchByTextAsync(string text)
{
var notes = this.dbContext.Notes
//.Include(l => l.Logbook)
// .ThenInclude(x => x.LogbookManagers)
//.Include(x => x.Logbook)
// .ThenInclude(s => s.Business)
//.Include(c => c.Category)
//.Include(u => u.User)
.Where(n => n.Text.Contains(text))
.ToList();

var mappedNotes = this.mappingProvider.MapTo<ICollection<NoteViewModel>>(notes);
return mappedNotes;
}

如果我简单地删除我做的四个包含,解析工作正常!我应该如何检索包含对象的集合?

Ajax 调用

$("#target").keyup(function (event) {
var request = $.ajax({
url: "/Management/Management/GetNotesAsyncJson",
type: "POST",
data: { "data": event.target.value },
dataType: 'json'
});

request.done(function (data) {
$.each(data, function (index) {
var textToPrepend =
"<div class='pricing-option' id='idPlace'>" +
"<i class='material-icons'> mode_comment</i>" +
"<h1 class='note-title' id='titlePlace'>admin@admin.admin</h1>" +
"<hr />" +
"<p id='textPlace'>" + data[index].text + "</p>" +
"<hr />" +
"<p id='priorityPlace'>" + data[index].prioritytype + "</p>" +
"<hr />" +
"<div class='price'>" +
"<div class='front'>" +
"<span class='note-title'>@Model.Category?.Name </span>" +
"</div>" +
"<div class='back'>" +
"<i class='material-icons' id='editNote' data-Id='@Model.Id'>edit</i>" +
"<i class='material-icons' id='deleteNote' data-Id='@Model.Id'>remove_circle</i>" +
"<i class='material-icons' id='archiveNote'>archive</i>" +
"</div>" +
"</div>" +
"</div>";

$('.pricing-table').prepend(textToPrepend)
});
})
request.fail(function (data) {
console.log(data);
console.log(data.responseText)
})
});

Controller


public JsonResult GetNotesAsyncJson(string data)
{
var notes = this.noteService.SearchByTextAsync(data);
var model = new SearchViewModel();
model.Notes = notes;

return Json(model.Notes);
}

最佳答案

在你的 Controller 中

[HttpPost({data})] 
public JsonResult GetNotesAsyncJson(string data)
{
var notes = this.noteService.SearchByTextAsync(data);
var model = new SearchViewModel();
model.Notes = notes;

return Json(model.Notes);
}

如果这不起作用,请尝试在您的 ajax 代码中使用 JSON.stringify,类似于这样

$("#performActionButton").click(function (event) {
var data = { data: event.target.value };
$.ajax({
url: '/url',
data: data,
type: 'POST',
traditional: true,
contentType: 'application/json; charset=utf-8',
success: function (data) {

}
});
});

关于c# - Json 无法解析 ajax post 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56573076/

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