2

I'm trying to use the DataTables library for Jquery to POST some simple data to a controller which is a SQL query. This query will return JSON and I will go on my merry way and then do what I will with the data.

I am having no problems with this when using built in jquery AJAX. Here is the working code:

$.post('/rx/dostuff', {fromDate: from_date, toDate: to_date}, function(data){                
            var dataObject = JSON.parse(data);
            if(data !== "[]"){
                $("#display_area").empty();
                $("#display_area").append("<div> " + ...

            blah blah blah

So this works great, I POST the from_date and the to_date, and I can see in the Network Params tab that it is being sent as

fromDate: 02/01/2016
toDate:   02/24/2016

which my PHP controller picks up, I do some statement binding to keep it safe, and boom, my results are returned to me JSON encoded.

When I try to do this using the DataTables library, my params end up being sent like this:

fromDate=02%2F01%2F2016&toDate=02%2F24%2F2016

PHP raises hell, and throws a warning that I am missing argument 1 for my controller.

My question is, why is DataTables sending the params like this? Have I malformed the way that it is meant to be sent?

Here is my DataTables AJAX code:

$('#dataTable').DataTable({
        ajax: {
            url: "/rx/dostuff",
            type: "POST",
            contentType: "application/json",
            data: {
                "fromDate": from_date,
                "toDate": to_date
            }
        },
        columns: [
            { data: 'col'},
            { data: 'col'},
            { data: 'col'},
            { data: 'col'}
        ]
    });

Any help is much appreciated!

1 Answer 1

1

Remove contentType: "application/json".

This parameter defines the format when sending data to the server. Default is "application/x-www-form-urlencoded; charset=UTF-8" which should be working for you according to what you've used before with $.post().

See $.ajax() for more information.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, that was indeed what was causing the weird post params. So simple!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.