简体   繁体   English

C# JObject 作为一个空列表出现在前面

[英]C# JObject arrives to the front as an empty list

I'm using jQuery Ajax to send a Newtonsoft object from a MVC controller task to the front, but I have been struggling a lot with it.我正在使用 jQuery Ajax 将 Newtonsoft 对象从 MVC 控制器任务发送到前端,但我一直在为此苦苦挣扎。 For some reason the object arrives as an empty list I tried to simplify the object thinking that the problem was the structure nesting other objects, but it doesn't work even in the most simple case.出于某种原因,对象作为一个空列表到达我试图简化对象,认为问题是嵌套其他对象的结构,但即使在最简单的情况下它也不起作用。 I updated the version of Newtonsoft as said in this other question Nested JObjects getting serialized as empty arrays but my problem persist.我更新了 Newtonsoft 的版本,如另一个问题Nested JObjects getting serialized as empty arrays 中所述,但我的问题仍然存在。 I know it looks easy but I'm not sure what I may be doing wrong.我知道这看起来很简单,但我不确定我做错了什么。 Here is the method in the controller这是控制器中的方法

[HttpPost]
public async Task<JsonResult> Something(string data)
{
            //some asynchronous stuff
            var jsonObject = new JObject();
            jsonObject.Add("x", "text");
            return Json(jsonObject);
}

My JQuery ajax call我的 JQuery ajax 调用

 $.ajax({
            type: "POST",
            url: url,
            data: JSON.stringify(parameters),
            contentType: "application/json",
            success: function (data) {
               debugger;
            }
        }).fail(function (jqXHR, textStatus, errorThrown) {          
});

And the answer arrives as somethign like this答案是这样的

[[[]]]

I'm going crazy with this problem any suggestions are really apreciated我对这个问题快疯了,任何建议都非常值得

I can' t see any async in your code, but in any case try this我在你的代码中看不到任何异步,但无论如何试试这个

public async Task<ActionResult> Something()
{
     return Ok( new { x="text"} );
}

I add an aswer to my own question hoping that it may help someone else in the same situation.我为我自己的问题添加了一个答案,希望它可以帮助处于相同情况的其他人。 I wrapped the answer in the following manner and now it works fine.我用以下方式包装了答案,现在它工作正常。 I'm not sure why it's not possible to send it directly but maybe is related to a problem on the nested objects.我不确定为什么不能直接发送它,但可能与嵌套对象上的问题有关。

JsonResult data = new JsonResult();
        data.Data = new
        {
            success = true,
            result = JsonConvert.SerializeObject(rta.Data)
        };

Also may be useful to be careful with the maxlength of the strings because that may add more problems if the original object is too big.小心字符串的 maxlength 也可能有用,因为如果原始对象太大,这可能会增加更多问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM