【问题标题】:Json failed to parse on ajax post requestJson 无法解析 ajax 发布请求
【发布时间】:2019-10-27 14:38:15
【问题描述】:

我有一个 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)
    })
});

控制器


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

            return Json(model.Notes);
        }

【问题讨论】:

    标签: c# json asp.net-core .net-core asp.net-core-mvc


    【解决方案1】:

    在你的控制器中

    [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) {
    
                }
            });
        });
    

    【讨论】:

    • 您的第一个选项 [FromBody] 与 Json 解析有什么关系?不管怎样,这两种方法都试过了,我什至没有收到控制器中的参数。
    • @TihomirTodorov 请再次检查我的答案
    • 不,我没有那样接收控制器中的参数。
    • 也许你可以看看这个blog
    • 我读过,但是,我认为这与我的问题无关。不过,感谢您提供帮助!
    【解决方案2】:

    解决方案:Json 处于自引用循环中,无法解析属性。我通过将 [JsonIgnore] 添加到导致此问题的属性来解决它。

    【讨论】:

      猜你喜欢
      • 2016-02-09
      • 2018-10-19
      • 2014-03-22
      • 2021-03-20
      • 2015-10-16
      • 2013-06-18
      • 2020-10-14
      • 2016-12-14
      • 1970-01-01
      相关资源
      最近更新 更多