0

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. 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. 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

 $.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

Sergio
  • 57
  • 2
  • 10
  • possibly unrelated: `data: JSON.stringify(parameters),` but your action doesn't have any parameters - are you sure it's the same action? Does your action return a value if you send it back hardcoded json? eg `return Content("{\"x\":\"text\"}");` – freedomn-m Nov 15 '21 at 16:54
  • 2
    What do you get when you debug the network connections? browser, F12, network will show you what was sent from the server before jquery attempts to de-json it (or add `dataType: 'text'` so that jquery doesn't parse it) – freedomn-m Nov 15 '21 at 17:03
  • Is this Asp.Net MVC 5? If so, your question is probably a duplicate of [JSON Objects are serialized to empty brackets when returned using JsonResult](https://stackoverflow.com/q/46058564/10263). – Brian Rogers Nov 19 '21 at 23:27
  • @BrianRogers yes you are right, that solution is also an alternative and without the need of wrapping the object. But may be problematic if the final string is too long – Sergio Nov 22 '21 at 16:13

2 Answers2

0

I can' t see any async in your code, but in any case try this. And it is better to use ActionResult that JsonResult. In both cases json will be returned, but ActionResult allows to return bad request if there are some problems

public async Task<ActionResult> Something()
{
     if(some promble) return BadRequest("problem");

     return Ok( new { x="text"} );
}
Serge
  • 40,935
  • 4
  • 18
  • 45
  • TY for your answer, my problem wasn't related to the async. Your test works fine but my problem was only with the JObject and not Json in general – Sergio Nov 22 '21 at 16:20
  • @Sergio I added some explanatios in my answer. – Serge Nov 22 '21 at 16:31
-1

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.

If the object is not that big this may be a better alternative as noted by JSON Objects are serialized to empty brackets when returned using JsonResult

Sergio
  • 57
  • 2
  • 10