Server-side processing - is it possible to send data as JSON instead of URL params?

Server-side processing - is it possible to send data as JSON instead of URL params?

PIKSTANGPIKSTANG Posts: 16Questions: 4Answers: 0

Out of the box, it looks like server-side processing will generate ajax calls that may look like this:
?draw=1&columns[0][data]=someColumnXYZ&columns[0][name]=&columns[0][searchable]=true&columns[0][orderable]=true&columns[0][search][value]=&columns[0][search][regex]=false

I'm interested in whether this information can be sent as JSON instead for easier processing on server side. For reference, I am looking to process these with a Java backend (Spring MVC). Having to parse this information manually seems quite inelegant. Is there a better way?

This question has accepted answers - jump to:

Answers

  • colincolin Posts: 15,112Questions: 1Answers: 2,583

    Hi @PIKSTANG ,

    Yep, if you use POST data, that will do it. See the example here.

    Cheers,

    Colin

  • PIKSTANGPIKSTANG Posts: 16Questions: 4Answers: 0
    edited October 2018

    Hi @colin,

    Thanks for the example. I gave it a try and found the default format is still sending as form data application/x-www-form-urlencoded. I ended up adding "contentType" : "application/json" as well as wrapping the data inside JSON.stringify() in order for it to be recognized as proper JSON data on the backend.

    Not sure if I'm on the right track here as these are modifications I had to make inside the DataTables library. Any suggestions for making this work without modifying jquery.dataTables.js? To clarify, mainly I'm concerned about having to wrap the data inside JSON.stringify().

    Thanks,
    Pik

  • kthorngrenkthorngren Posts: 20,140Questions: 26Answers: 4,735
    Answer ✓

    See if ajax.data will work for you. Something like the last example in the docs.

    Kevin

  • allanallan Posts: 61,438Questions: 1Answers: 10,049 Site admin
    Answer ✓

    Yup - what Kevin suggests sounds like exactly what you want. The last example shows how you can have the data send as JSON in the request body.

    Allan

  • PIKSTANGPIKSTANG Posts: 16Questions: 4Answers: 0

    Perfect! Thanks everyone :smile:

  • 123adsens123adsens Posts: 4Questions: 1Answers: 0
    edited April 2020

    When sending data like this , the pagination parameteres are not working, like start , limit and sort. Any solution for this ?

  • kthorngrenkthorngren Posts: 20,140Questions: 26Answers: 4,735

    Please don't duplicate questions. See my response in your other thread.

    Kevin

This discussion has been closed.