0

I´m trying to create a datatable using Ajax, but before creating and filling the data table with the ajax response, on success I send an alert with "response.d" it returns undefined and thats why im not filling the data table.

Script

<script>
        $(document).ready(function () {
            $('#tabHistorialTrabajos').click(function () {
                $.ajax({
                    type: "GET",
                    url: "/Monitoreo/GetData",
                    data: '{}',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    //success: OnSuccess,
                    success: function (response) {
                        alert(response.d);
                    },
                    failure: function (response) {
                        alert(response.d);
                    },
                    error: function (response) {
                        alert(response.d);
                    }
                });
            });
        });

        function OnSuccess(response) {
            $("datatableTrabajos").DataTable(
                {
                    blengthchange: true,
                    lengthmenu: [[5, 10, -1], [5, 10, "all"]],
                    bfilter: true,
                    bsort: true,
                    bpaginate: true,
                    data: response.d,
                    columns: [{ 'data': 'idTrabajo' },
                        { 'data': 'idTurnoXMaquina' },
                        { 'data': 'parte' },
                        { 'data': 'cliente' },
                        { 'data': 'cantidadPedido' },
                        { 'data': 'cantidadMerma' },
                        { 'data': 'cantidadTarimas' },
                        { 'data': 'fechaInicio' },
                        { 'data': 'fechaFin' },
                        { 'data': 'cantidadFabricado' },
                        { 'data': 'tiempoMuerto' },
                        { 'data': 'velocidad' },
                        { 'data': 'fabricado' }
                    ]
                });
        };
    </script>

Controller (returns a JSON)

public string GetData()
        {
            ServiciosMenu serviciosMenu = new ServiciosMenu();
            List<CorridaModel> listadoTrabajosXMaquina = serviciosMenu.GetHistorialTrabajosXMaquina(4);
            string JsonResult = JsonConvert.SerializeObject(listadoTrabajosXMaquina);
            return JsonResult;
        }
freedomn-m
  • 27,664
  • 8
  • 35
  • 57
Yair Briones
  • 41
  • 1
  • 7
  • [Please do not upload images of code/data/errors when asking a question.](//meta.stackoverflow.com/q/285551) – Heretic Monkey May 06 '22 at 16:47
  • So what's the value of `response`? Why would it be `.d`? – freedomn-m May 06 '22 at 16:55
  • Sorry, i´m a little nooby in the use of jquery/ajax. I saw it on a article where they teach how to use datatable with ajax and they use .d to access data – Yair Briones May 06 '22 at 17:00
  • No worries, it'll be whatever you create it as from your GetData() server method. Most likely, if you're providing the correct format, you can use just `data: response,` - but datatables will need the data in the correct format and it's unclear (unlikely?) if `GetHistorialTrabajosXMaquina()` is formatting it as required by datatables. – freedomn-m May 06 '22 at 17:16
  • My GetData controller is returing only a JSON, and that JSON I´m trying to assigne it to the data of DataTable – Yair Briones May 06 '22 at 17:31
  • Yes, but "JSON" contains data, you can't just randomly assign "data" and hope everything works. – freedomn-m May 06 '22 at 18:20
  • Yes I understand, i make a test and print at console my "response" it contains an array of 98 elements, so the controller is returning the information in a correct format. – Yair Briones May 06 '22 at 18:34
  • If you need further help, pls let me know. – Jason Pan May 09 '22 at 04:43

1 Answers1

0

We need to change the default value of maxJsonLength. I learned from github that the default value of this maxJsonLength is 30M.

And suggestion for yor method in Controller:

public JsonResult GetData()
{
    List<TestPersonModel> list = new List<TestPersonModel>();
    TestPersonModel m = null;
    for (int i = 0; i < 10; i++)
    {
        m = new TestPersonModel();
        m.name = "John" + i;
        m.age =  i+1;
        m.car = "car" + i;
        list.Add(m);
    }
    return Json(list);
}
public class TestPersonModel { 
    public string name { get; set; }
    public int age { get; set; }
    public string car { get; set; }
}

Under normal circumstances, we do not return string types, but generally return Json types.

Since the length of the string of your return type is not clear, it is possible that the current problem can be solved by the above code.

If it doesn't work, it is recommended to refer to the link below, or provide the code for the smallest unit that can be reproduced.

Max Length for Json Object Asp.net Core 3.1

Jason Pan
  • 15,263
  • 1
  • 14
  • 29