Server-Side Processing: Send Request via JSON

Server-Side Processing: Send Request via JSON

kaseykrehbielkaseykrehbiel Posts: 18Questions: 2Answers: 0
edited November 2012 in General
Using this example:
http://datatables.net/release-datatables/examples/data_sources/server_side.html

When I click the Next button on the table, the request header looks like this:
[code]
GET /release-datatables/examples/server_side/scripts/server_processing.php?sEcho=3&iColumns=5&sColumns=&iDisplayStart=20&iDisplayLength=10&mDataProp_0=0&mDataProp_1=1&mDataProp_2=2&mDataProp_3=3&mDataProp_4=4&sSearch=&bRegex=false&sSearch_0=&bRegex_0=false&bSearchable_0=true&sSearch_1=&bRegex_1=false&bSearchable_1=true&sSearch_2=&bRegex_2=false&bSearchable_2=true&sSearch_3=&bRegex_3=false&bSearchable_3=true&sSearch_4=&bRegex_4=false&bSearchable_4=true&iSortCol_0=0&sSortDir_0=asc&iSortingCols=1&bSortable_0=true&bSortable_1=true&bSortable_2=true&bSortable_3=true&bSortable_4=true&_=1353960845704 HTTP/1.1
[/code]

My question is this: Is there any way built into dataTables that allows me to send this data via JSON instead of in the URL string? Something like:
[code]
{
"sEcho": "3",
"iColumns": "5",
"iDisplayStart": 20 // etc.
}
[/code]

Replies

  • allanallan Posts: 63,107Questions: 1Answers: 10,394 Site admin
    You mean an HTTP parameter which itself is a string, but that string is a JSON object? Sure, its a bit unusual since you need to access an HTTP parameter anyway, but you can use fnServerData to override the default Ajax behaviour of DataTables and replace the $.ajax call with whatever you want (specifically a `data` parameter which is a JSON encoded string in this case I'd guess).

    Allan
  • kaseykrehbielkaseykrehbiel Posts: 18Questions: 2Answers: 0
    Hi Allan,
    Thanks for your response. After I wrote this question, I learned that you can use POST instead of GET, and that gets me halfway there, but I'd like to be able to post JSON instead of sending the data through the query string. I don't want to send it as an HTTP parameter, but a normal string in the "responseText". (In this case it'd be the opposite of responseText as I'm sending it to the server, but the same concept nonetheless.)

    What I'm trying to do overall is setup a WCF service that accepts JSON as input and returns JSON as output in order to fill up the datatable. I'm doing that everywhere else on the site I'm working on and I want to do it with this plugin as well. Does fnServerData allow me to do that?

    By the way, I just want to say a huge thank you to you for all of your work on this plugin. It is absolutely awesome, and I've been using it for several years across many projects and platforms and it just gets better and better. I really appreciate it!
  • allanallan Posts: 63,107Questions: 1Answers: 10,394 Site admin
    > I don't want to send it as an HTTP parameter, but a normal string

    An HTTP parameter is a normal string. You can't send parameters over HTTP without using HTTP parameters! You must(*) do something like:

    [code]
    variable1 = whatever1
    ... etc
    [/code]

    You could have `whatever1` as a JSON string if you want.

    (*) Actually you can just set the body of the request if you want. Have a look around Google, but I'd imagine its unlikely that you want to do that unless you are really quite sure of it, or are uploading files etc.

    Allan
  • kaseykrehbielkaseykrehbiel Posts: 18Questions: 2Answers: 0
    May I ask why I would not want to do that? That's how the response comes back, isn't it? With the JSON in the body?
  • allanallan Posts: 63,107Questions: 1Answers: 10,394 Site admin
    Because your server environment will be setup to handle HTTP parameters. For example in PHP you might use `$_GET['variable']` or `$_POST['variable']` . Speciality in very other language that provides easy access to HTTP variables. You could read directly from stdin (assuming the web-server provides access to that directly - they typically will) and use a POST or PUT with body content set and then parse it yourself, but that seems like a huge amount of work to me which you can simple use the methods which are already proven to work.

    > That's how the response comes back, isn't it?

    Yes, but the key difference is between making the request and what comes back.

    Allan
This discussion has been closed.