I tried this
>>> data = '{"input":[{"the_owner":1,"category":1,"name":"La marcha del
oro","price":44.45,"id":1,"quantity":100},{"the_owner":3,"category":3,"name":"Toy
Story","price":34.56,"id":2,"quantity":23},{"the_owner":2,"category":2,"name":"La
Sirenita","price":56.67,"id":3,"quantity":10},{"the_owner":null,"category":null,"name":null,"price":null,"id":null,"quantity":null}]}'
>>> import json
>>> json.loads(data)
{u'input': [{u'the_owner': 1, u'category': 1, u'name': u'La marcha del
oro', u'price': 44.45, u'id': 1, u'quantity': 100}, {u'the_owner': 3,
u'category': 3, u'name': u'Toy Story', u'price': 34.56, u'id': 2,
u'quantity': 23}, {u'the_owner': 2, u'category': 2, u'name': u'La
Sirenita', u'price': 56.67, u'id': 3, u'quantity': 10}, {u'the_owner':
None, u'category': None, u'name': None, u'price': None, u'id': None,
u'quantity': None}]}
and it works form me. Is it possible you are calling the wrong json? Make
sure you do not have
from gluon.serializers import json # not ok this json = simplejson.dumps
in the controller or in model but instead
import json #ok
or
import simplejson as json #ok
On Friday, 20 February 2015 20:43:33 UTC-6, clara wrote:
>
> Hello all,
>
> Thanks all for the responses and sorry for the delay in my response. I was
> struggling with this and I don't get to solve it yet. The data (string)
> passed to the controller as *request.vars.input *looks good but it fails
> when I try to load it with json.loads.
>
> This is what I have at this point:
>
> *Controller that is passed to ajax call:*
>
> def test():
> print "test called"
> print '-'*40
> print 'request.vars:'
> print request.vars
> print '-'*40
> print 'request.vars.input:'
> print request.vars.input
> print '-'*40
> print type(request.vars.input)
> try:
> data = json.loads(request.vars.input)
> except:
> print "json.loads failed"
> return 'ok'
>
> *Print-out when test controller is called by ajax:*
> test called
> ----------------------------------------
> request.vars:
> <Storage {'input': '{"input":[{"the_owner":1,"category":1,"name":"La
> marcha del oro","price":44.45,"id":1,"quantity":100
> },{"the_owner":3,"category":3,"name":"Toy
> Story","price":34.56,"id":2,"quantity":23},{"the_owner":2,"category":2,"name":
> "La
> Sirenita","price":56.67,"id":3,"quantity":10},{"the_owner":null,"category":null,"name":null,"price":null,"id":null,"
> quantity":null}]}'}>
> ----------------------------------------
> request.vars.input:
> {"input":[{"the_owner":1,"category":1,"name":"La marcha del
> oro","price":44.45,"id":1,"quantity":100},{"the_owner":3,"ca
> tegory":3,"name":"Toy
> Story","price":34.56,"id":2,"quantity":23},{"the_owner":2,"category":2,"name":"La
>
> Sirenita","price
>
> ":56.67,"id":3,"quantity":10},{"the_owner":null,"category":null,"name":null,"price":null,"id":null,"quantity":null}]}
> ----------------------------------------
> <type 'str'>
> False
> json.loads failed
>
> *And the JS code (I highlghted the important part)*
>
> {{extend 'layout.html'}}
> {{from gluon.serializers import json}}
>
> <script type="text/javascript">
> {{=ASSIGNJS(data = data)}}
> {{=ASSIGNJS(grid = grid)}}
>
> $(document).ready(function () {
> var
> container1 = document.getElementById('example1'),
> settings1 = {
> data: grid,
> minSpareRows: 1,
> contextMenu: true,
> },
> hot1;
>
> hot1 = new Handsontable(container1, settings1);
> hot1.render();
> var test = {'myvar':'soy una variable'};
> $("#save").click(function()
> {var tableData = JSON.stringify(hot1.getData());
> var input = JSON.stringify({"input":
> hot1.getData()});
> * $.ajax({*
> * url:'mygrid/test',*
> * data: {'input':input},*
> * datatype: 'json',*
> * type: 'post'*
>
> * });*
> alert(input);});
>
> });
> </script>
>
> <div id="example1" class="handsontable"></div>
> <button name="save" id="save">Save</button>
> <div id="target"></div>
>
> I will keep looking into it. If anyone has any ideas or tests, I will try
> them.
>
> Thanks again,
>
> C.
>
>
> El viernes, 20 de febrero de 2015, 15:19:55 (UTC-3), clara escribió:
>>
>> Hello,
>>
>> I am making an Ajax call and calling a controller with the json data I
>> need to process in the controller.
>>
>> The problem I am having (and I am sure the answer will be simple and I
>> will hate myself for the silly question ) is that if I select
>> request.vars.input, its value is not a json object anymore so a loads
>> method will fail. It is a string object and if I try to eval(it) I get an
>> error. So even when the data is right there I can not use it in the
>> controller.
>> Any ideas will be more than welcome!
>>
>> (The controller and its print outs are shown bellow.)
>>
>> Thanks,
>> Clara
>>
>> def test():
>> print "test called"
>> print "test called"
>> print 'request.vars'
>> print request.vars
>> print 'request.vars.input'
>>
>> Here is what the controller prints out:
>>
>> test called
>> request.vars
>> <Storage {'input': '[{"the_owner":1,"category":1,"name":"La marcha del
>> oro","price":44.45,"id":1,"quantity":100},{"the_owner":3,"c
>> ategory":3,"name":"Toy
>> Story","price":34.56,"id":2,"quantity":23},{"the_owner":2,"category":2,"name":"La
>>
>> Sirenita","price":56.67,"
>>
>> id":3,"quantity":10},{"the_owner":null,"category":null,"name":null,"price":null,"id":null,"quantity":null}]'}>
>> request.vars.input
>> [{"the_owner":1,"category":1,"name":"La marcha del
>> oro","price":44.45,"id":1,"quantity":100},{"the_owner":3,"category":3,"name":"T
>> oy
>> Story","price":34.56,"id":2,"quantity":23},{"the_owner":2,"category":2,"name":"La
>>
>> Sirenita","price":56.67,"id":3,"quantity":10}
>>
>> ,{"the_owner":null,"category":null,"name":null,"price":null,"id":null,"quantity":null}]
>>
>>
>>
--
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.