Search code examples
djangowagtailtabulator

Tabulator ajaxURL adding a question mark to end


I am using wagtail/django for a project and am trying to do ajax calls to tabulator. I currently have this

var table = new Tabulator("#example-table", {
    ajaxURL:"http://localhost:8000/api/v2/people/?fields=*", //ajax URL
    ajaxConfig:{
        method:"get", //set request type to Position
        headers: {
            "Content-type": 'application/json; charset=utf-8', //set specific content type
        },
    },
    height:"311px",
    layout:"fitColumns",
    placeholder:"No Data Set",
    columns:[
        {title:"Name", field:"name", sorter:"string", width:200},
        {title:"Date", field:"date", sorter:"string"},
    ],
});

table.setData();

But my debug console in the browser has

Request URL:http://localhost:8000/api/v2/people/?fields=*?
"message": "fields error: unexpected char '?' at position 1"

Why is a question mark being added to the end? I can replicate the same error if I add a question mark to the end in the Django Rest Page.


Solution

  • The ? is being added because that's the standard way to pass additional parameters to the URL, and apparently Tabulator isn't checking whether ajaxURL has one already. You can fix this by moving the fields parameter to ajaxParams, where it will be combined with any other parameters that Tabulator might pass:

    var table = new Tabulator("#example-table", {
        ajaxURL:"http://localhost:8000/api/v2/people/",
        ajaxParams: {'fields': '*'},
        ajaxConfig:{
            method:"get", //set request type to Position
            headers: {
                "Content-type": 'application/json; charset=utf-8', //set specific content type
            },
        },
        height:"311px",
        layout:"fitColumns",
        placeholder:"No Data Set",
        columns:[
            {title:"Name", field:"name", sorter:"string", width:200},
            {title:"Date", field:"date", sorter:"string"},
        ],
    });