Uncaught TypeError: Cannot read property 'length' of undefined with AJAX Resonse

Uncaught TypeError: Cannot read property 'length' of undefined with AJAX Resonse

5320835@qq.com5320835@qq.com Posts: 15Questions: 5Answers: 0

Hi,

I am facing a issue above when I update a column on data tables, here is my client side code

var editor = new $.fn.dataTable.Editor({
    ajax: {
        url: "http://localhost/sql/RaymSP_GatewayPaymentOrder",
        type: "POST",
        contentType: "application/json; charset=utf-8",
        data: function (d) {
            var d = [{
                "username": 'john',
                "method": 2,
                "storeid": Number(editor.field('store_id').val()),
                "ordernumber": editor.field('OrderNumber').val(),
                "paymentmethod": editor.field('PaymentMethod').val(),
                "statusdescription": editor.field('Status').val()
            }]
            return JSON.stringify(d);
        },
        dataType: "json",
        dataSrc: "ResultSets"
    },

The server response would like this
{
"ResultSets": [],
"OutputParameters": {},
"ReturnValue": 0
}

http://debug.datatables.net/iqewab

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,351Questions: 1Answers: 10,443 Site admin

    It looks like the response from the server doesn't match what Editor expects. Are you able to update your returned JSON to match what Editor expects?

    Regards,
    Allan

  • 5320835@qq.com5320835@qq.com Posts: 15Questions: 5Answers: 0

    Hi Allan, Thanks for your commits, I'm not be able to change server side settings, is there any way to to on datatables?
    Regards,
    Wenbin

  • allanallan Posts: 63,351Questions: 1Answers: 10,443 Site admin
    edited October 2016

    You can use the postSubmit event to modify the data being returned from the server into what Editor requires. Unfortunately, Editor does require certain information back from the server - there is no way around that.

    Allan

  • 5320835@qq.com5320835@qq.com Posts: 15Questions: 5Answers: 0

    Dear Allan,

    I've tested this event today but doesn't work, the event even didn't not be called when the error message occur, I tried the other ways but also doesn't work, there are dataSrc, ajax function and ajax object with success and complete. the last code here is below
    Now there is no error message occur however the editor looks like freeze when I clicked Update button, no response and I cannot open editor form again from table after I close it, I have to reload whole page.

        ajax: function (method, url, data, success, error) {
            var param = [{
                "username": 'john',
                "method": 2,
                "storeid": Number(editor.field('store_id').val()),
                "ordernumber": editor.field('OrderNumber').val(),
                "paymentmethod": editor.field('PaymentMethod').val(),
                "statusdescription": editor.field('Status').val()
            }]
            $.ajax({
                url: "http://localhost/sql/RaymSP_GatewayPaymentOrder",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(param),
                success: function (c) {
                    c.data = c[0].ResultSets[0]
                    success(c.data);
    

    As you can see, my server has JSON response however there is no data as name inside and unfortunately to change the response is very complex.
    Can we manipulate server response in AJAX in datatable, if yes, can you give me a similar example as I am very new for programming.

    Many Thanks,
    Wenbin

  • 5320835@qq.com5320835@qq.com Posts: 15Questions: 5Answers: 0
    edited October 2016

    Hi Allan,

    Sorry the error message still there as below
    dataTables.editor.js:formatted:3594 Uncaught TypeError: Cannot read property 'length' of undefined

    Here is server response
    {
    "ResultSets": [
    [
    {
    "store_id": 3,
    "store_name": "鲁班路店",
    "OrderNumber": "3082936",
    "OrderType": "MOSAIC",
    "OrderDate": "2016-10-13 10:26:12",
    "subject": "MPOS订单编号3082936",
    "TotalAmount": 0.01,
    "Discount": 0,
    "Tax": 0,
    "ShippingFee": 0,
    "CustomerName": null,
    "PhoneNumber": null,
    "Status": "已关闭",
    "PaymentMethod": "微信"
    }
    ]
    ],
    "OutputParameters": {},
    "ReturnValue": -1
    }

    However there is ResultSets instead of data in server response.

    Please help,

    Thanks,
    Wenbin

  • allanallan Posts: 63,351Questions: 1Answers: 10,443 Site admin
    Answer ✓

    You should be able to use:

    editor.on( 'postSubmit', function ( e, json ) {
      json.data = json.ResultSets;
    } );
    

    If that isn't working for you, can you give me a link to the page showing the issue so I can help to debug it.

    Allan

  • 5320835@qq.com5320835@qq.com Posts: 15Questions: 5Answers: 0

    Hi Allan,

    Thanks for your reply, as you know it's mid night here, so I will test it tomorrow morning and let you know,

    Thanks again for your support,

    Regards,
    Wenbin

  • 5320835@qq.com5320835@qq.com Posts: 15Questions: 5Answers: 0

    Dear Allan,

    I just verified this method and it works, that's fantastic, you know I've been working on this issue for last couple days and finally it solved, thank you so much for this big help...

    Regards,
    Wenbin

This discussion has been closed.