Doing a POST to API with ajax.data as a STRING vs. FUNCTION()

Doing a POST to API with ajax.data as a STRING vs. FUNCTION()

rldean1rldean1 Posts: 141Questions: 66Answers: 1

Why won't specifying a string for POSTing data in ajax.data work, but doing so as a data: function() {} will?

Something strange happens to the Request Payload. The documentation, I think, said something about DT creating an object here for internal use.

Using Postman, I can specify several different content types, and my API accepts it -- I guess it's pretty forgiving. It will even accept data that as been encoded with encodeURIComponent(xml). However, DT's ajax.data may do something weird to the request. Check this out...

As a String (does not work)
For contentType, I've tried application/xml, text/plain, and text/xml. Look what happens to the Request Payload. I don't get it.

tblEffectiveDates = $('#tblEffectiveDates').DataTable({
    ajax: {
        url: "https://superSecretURL",
        type: "POST",
        data: '<xml><AppAction><text>getEffectiveDates</text></AppAction></xml>',
        contentType: "text", //posting data type
        dataType: "xml", //returning data type
    }
});

This is what happens to the Request Payload:

0=%3C&1=x&2=m&3=l&4=%3E&5=%3C&6=A&7=p&8=p&9=A&10=c&11=t&12=i&13=o&14=n&15=%3E&16=%3C&17=t&18=e&19=x&20=t&21=%3E&22=I&23=N&24=I&25=T&26=%3C&27=%2F&28=t&29=e&30=x&31=t&32=%3E&33=%3C&34=%2F&35=A&36=p&37=p&38=A&39=c&40=t&41=i&42=o&43=n&44=%3E&45=%3C&46=%2F&47=x&48=m&49=l&50=%3E

As a Function (works):
So, then, I tried doing it as a function. If I return the string through the function, everything is cool. ALSO, I can use any of the contentType's previously mentioned. I can even use encodeURIComponent() with it.

tblEffectiveDates = $('#tblEffectiveDates').DataTable({
    ajax: {
        url: "https://superSecretURL",
        type: "POST",
        data: function () {
            return '<xml><AppAction><text>geteffectivedates</text></AppAction></xml>';
        },
        contentType: "text", //posting data type
        dataType: "xml", //returning data type
    }

});

Notice that the Request Payload is NOT modified:

<xml><AppAction><text>geteffectivedates</text></AppAction></xml>

(or, if encodeURIComponent() was used)

%3Cxml%3E%3CAppAction%3E%3Ctext%3E%3C!%5BCDATA%5BgetEffectiveDates%5D%5D%3E%3C%2Ftext%3E%3C%2FAppAction%3E%3C%2Fxml%3E

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,689Questions: 1Answers: 10,500 Site admin
    Answer ✓

    Yes this is actually expected in DataTables. DataTables has an object that contains the data it needs to send to the server, which is not compatible with string (how do you add the object to the string?). When ajax.data is used as a function though it will take whatever you return and use that (assuming that you've combined the data it wants to submit with its own object).

    This is the relevant block of code.

    Also worth noting that the documentation doesn't say that ajax.data would take a string :).

    Allan

This discussion has been closed.