1

I am sending an array of objects in a POST via AJAX, but I always get my object as empty, before that my method was not accessed via AJAX so I added a header to my request and started working, but the object is always null.

Ajax post method:

this.SaveFluxo = function () {

var data = JSON.stringify(objThat.arrFluxoSave);

$.ajax({
    type: "POST",
    url: "/save-fluxo",
    data: data,
    contentType: 'application/json',
    dataType: "JSON",
    headers: { "RequestVerificationToken": $('input[name="__RequestVerificationToken"]').val() },
    success: function (response) {

        processResponse(response);
    }
});

}

Controller method:

    [HttpPost]
    [Route("save-fluxo")]
    public IActionResult SaveFluxo([FromBody] ItemGroup items)
    {
        Process(items);

        return Json(new
        {
            state = 1935,
            msg = "success"
        });
    }

My classes:

public class ItemGroup
{
    public IList<Item> Items { get; set; }
}

public class Item
{
    public int Tipo { get; set; }
    public string IdRito { get; set; }
    public int IdFase { get; set; }
    public int PosicaoFluxo { get; set; }
    public bool Concluido { get; set; }
}

enter image description here

Renan Barbosa
  • 1,046
  • 3
  • 11
  • 31

2 Answers2

0

After checking this answer, i removed the class ItemGroup and changed the parameter:

public IActionResult SaveFluxo([FromBody] ItemGroup items)

To receive an array:

public IActionResult SaveFluxo([FromBody] Item[] items)
Renan Barbosa
  • 1,046
  • 3
  • 11
  • 31
0

Your model may just not be compatible with the JSON payload being sent. Check for things like arrays vs. objects.

One quick trick is to try to manually deserialize the object and look for errors.

Here I just use [FromBody] dynamic payload to make sure I'm actually getting a JSON object. If you aren't getting this far then the problem is probably in your client JSON creation logic.

    [AllowAnonymous]
    [HttpPost("submitGoogleForm")]
    public async Task<ActionResult> SubmitGoogleForm([FromBody] dynamic payload) 
    {
        var obj = JsonConvert.DeserializeObject<GoogleFormResponses>(payload.ToString());
        return Ok();
    }

This immediately gave me this exception:

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'RR.API.Controllers.GoogleFormModel' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.

I then realized my JSON looked like this:

{
  "form": [
    {
      "type": "TEXT",
      "id": 2062694622,
      "ix": 0,
      "question": null
    },

But my model was:

   class GoogleFormResponses {
       public GoogleForm Form { get;set; }
   }

   class GoogleForm {
       // This was empty and I still got errors!
   }

And then I realized I was sending an array instead of an object for the form property.

Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689