POSTing JSON data but need to manipulate data

POSTing JSON data but need to manipulate data

dshorthousedshorthouse Posts: 2Questions: 1Answers: 0

I am using CouchDB 2.1 and am trying to make use its built-in _find. This requires that I POST JSON data and so my ajax.data object is a function to return JSON.stringify. However, I also have a need to manipulate the data object just before POST using the preXhr.dt event. But, because the data object is now a string, I cannot manipulate it. Any suggestions?

Answers

  • colincolin Posts: 2,365Questions: 0Answers: 425

    Hi @dshorthouse ,

    Can't you just cast it back to a JSON structure with JSON.parse(), do your manipulating, and then reconvert with JSON.stringify() again?

    Cheers,

    Colin

  • dshorthousedshorthouse Posts: 2Questions: 1Answers: 0

    Thanks, @colin. Unfortunately, pre-/post-processing of data in $ajax DataTables assumes these are objects, not strings. I've had to hack the js file to get this to work & don't see how I can send a pull request for this because it would break the examples provided for POSTing JSON data, https://github.com/dshorthouse/orcid-taxonomist-couchdb/blob/master/public/js/jquery.dataTables.custom.js#L3937

  • allanallan Posts: 48,053Questions: 1Answers: 6,898 Site admin

    The preXhr event happens after the ajax.data function is called. So if you need to manipulate the data before converting it to be a JSON string you would have to do it inside your ajax.data function.

    Is that something that you can do?

    Allan

Sign In or Register to comment.