Quantcast
Channel: DataTables 1.10 — DataTables forums
Viewing all articles
Browse latest Browse all 2366

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

$
0
0

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

Viewing all articles
Browse latest Browse all 2366

Trending Articles