In jQuery, if you do:
data: {json1: JSON.stringify([1, 2, 3])},
contentType: 'application/json'
Then in the controller, request.post_vars.json1 will be the Python list, [1,
2, 3] -- no need to do any JSON conversion on the server side. Note, the
JSON must be an object with keys, not simply an array -- otherwise web2py
will not treat it as JSON and convert it to a Python data structure. If the
JSON has keys, the keys will become the keys of request.post_vars.
Anthony
On Sunday, February 4, 2018 at 9:41:48 PM UTC-5, lucas wrote:
>
> hey all and anthony,
>
> i've tried a bunch of things, including your suggestion above. here are
> my results and this is using web2py 2.16.1. on the client side, under the
> view's header, i have:
>
> a = getArray();
> //displays the grabbed array
>
> //jQuery('div#snapshot_matrix').html(snapshot_table+'<br/><pre>'+JSON.stringify(a,
>
> null, '\t')+'</pre>').css('display', 'block');
> jQuery.post({url:"{{=URL(c='main321911',
> f='ajaxAnalysesSummary1')}}", data:{ 'json1':JSON.stringify([1, 2, 3]) }
> }).done(function (t) {
>
> //jQuery('div#snapshot_matrix').html('<pre>server_return:\n'+JSON.stringify(t,
>
> null, '\t')+'</pre>').css('display', 'block');
>
> jQuery('div#snapshot_matrix').html('<pre>server_return:\n'+t+'</pre>').css('display',
>
> 'block');
>
> where a or getArray() just reads the values of various inputs and slams
> them into a javascript/json object with the declaration "var a = { };"
> which to me is similar to a python dict. the jQuery.post is the method i
> used to transfer the array to the server and under the post function i've
> tried various permutations on data with and without the option
> ..."contentType: 'application/json', "... present or not. the various
> permutation of data were:
>
> data: a,
> data: JSON.stringify(a),
> data: { json1:[1, 2, 3] },
> data: { 'json1':[1, 2, 3] },
> data: { json1:JSON.stringify([1, 2, 3]) },
> data: { 'json1':JSON.stringify([1, 2, 3]) },
>
> and many others. on the server side, i just have:
>
> def ajaxAnalysesSummary1():
> #from gluon.contrib import simplejson
> dct = { 'ajaxAnalysesSummary1':True }
> get = request.vars
> dct['len1'] = len(get)
> dct['type1'] = type(get)
> dct['get1'] = get
> get = request.post_vars
> dct['len2'] = len(get)
> dct['type2'] = type(get)
> dct['get2'] = get
>
> get = loads(request.vars.json1)
> dct['len3'] = len(get)
> dct['type3'] = type(get)
> dct['get3'] = get
> get = request.post_vars.json1
> dct['len4'] = len(get)
> dct['type4'] = type(get)
> dct['get4'] = get
> return DIV(dct)
>
> where the client will display the dct of type dict passed to DIV.
>
> the only way that consistently works is when i use JSON.stringify(a) on
> the client side, both 2nd and last, worked under the above data options,
> and loads on the server side. the loads will convert the pure "a" json
> object, in the 2nd data option, to a proper addressable python dict, or as
> json1 variable converted with the python dict under that variable. the
> values of the dict still have to be processed into int or double depending
> on their twins on the db side.
>
> but in every permutation, setting the option of "contentType:
> 'application/json'" returned no data under request.vars or
> request.post_vars.
>
> so, i believe i go with the working model of JSON.stringify on the client
> side with loads on the server side. makes sense and i believe a solid
> solution.
>
> lucas
>
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.