Invalid JSON response when calling success callback with empty object

Invalid JSON response when calling success callback with empty object

grizzlyygrizzlyy Posts: 6Questions: 2Answers: 0

Hello,

I'm trying to use localStorage as the data source, as shown in this example.

However, I have a problem to implement the remove action. I get an "invalid JSON response" error after calling the successCallback either with an empty data attribute, like in the linked example (success({data: []})), or with a completely empty object, like shown in the protocol definition (success({})).

Magic doesn't happen at lines 20-25:

                            linkCampaignToProjectEditorConfig['ajax'] = function (method, url, data, success, error) {
                                var project = getProjectUnderCreation();

                                if (data.action === 'create') {
                                    var output = { data: [] };
                                    var campaignId = data.data[0]['campaign'];

                                    // TODO: remove async false
                                    var campaign = $.ajax({
                                        async: false,
                                        url: 'api/campaigns/' + campaignId
                                    }).responseJSON;

                                    project['campaigns'].push(campaign);
                                    localStorage.setItem('projectUnderCreation', JSON.stringify(project));

                                    output.data.push({'campaign': campaign});
                                    success(output);

                                } else if (data.action = 'remove') {
                                    var campaignId = Object.keys(data.data)[0];
                                    project.campaigns = project.campaigns.filter(c => c.id != campaignId);
                                    localStorage.setItem('projectUnderCreation', JSON.stringify(project));
                                    
                                    success({}); // -> invalid JSON, same with success({data: []})
                                }
                            }

Side note: I also have the same problem with the create action, despite trying various changes to comply with the communication protocol. However, for now I try to make the remove action not show the JSON error, which should be easier.

Thx in advance for your help,
Alex

This question has an accepted answers - jump to answer

Answers

  • tangerinetangerine Posts: 3,342Questions: 35Answers: 394
    } else if (data.action = 'remove') {
    

    That's an assignment, not a comparison.

  • grizzlyygrizzlyy Posts: 6Questions: 2Answers: 0
    edited September 2020

    Thanks for pointing this error out, however it doesn't seem to be the cause of the JSON problem.

    To be more specific, I went into this conditional block in debug mode and I'm sure the callback is called with either of the two alternatives I checked.

  • kthorngrenkthorngren Posts: 20,141Questions: 26Answers: 4,736
    Answer ✓

    Possibly you are running into another error? Have you looked at your browser's console. for errors?

    Please post a link to your page or a running test case so we can help debug.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • grizzlyygrizzlyy Posts: 6Questions: 2Answers: 0

    So actually it was indeed due to another error (not sure exactly what, since I fixed several things I thought irrelevant to this problem, but possibly it had to do with JQuery selector and nested tables).

    Alex

This discussion has been closed.