Problem passing a json object to controller

54 views
Skip to first unread message

Patito Feo

unread,
Nov 20, 2019, 12:08:03 PM11/20/19
to web2py-users
Hi,

Ive been trying to pass an object with keys to the controller. But i havent been able to extract the request.vars.

Here is my view:

var Cars = {}
Cars.make = 'Ford';
Cars.model = 'Mustang';
Cars.year = 1969;

$.post( "{{=URL('default', 'transcriptor')}}", //request.post_vars.json1
   {     
         tarea: 'solicitudguardar', items: { json1: JSON.stringify( Cars ) }
   }, )


Here is my function:

elif request.vars.tarea == 'solicitudguardar':

   var = json.loads ( request.post_vars.items[json1] )
       
       

Here is one of the errors i got:

global name 'json1' is not defined



I dont understand why the array object is not being process as a json array in controller. 

Any help will be much appreciated.


Cheers,

Javier Pepe

unread,
Nov 20, 2019, 12:35:38 PM11/20/19
to web...@googlegroups.com
You need use:

   var = json.loads ( request.post_vars.items["json1"] )

--
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 web2py+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/cb71d997-01f3-4dd3-b689-35c280e035b7%40googlegroups.com.

Patito Feo

unread,
Nov 20, 2019, 1:38:17 PM11/20/19
to web2py-users
Javier, 

Tested and not working.

<type 'exceptions.TypeError'> 'builtin_function_or_method' object has no attribute '__getitem__'


request.post_vars  <Storage {'tarea': 'solicitudguardar', 'items[json1]': '[1,2,3]'}>


Any ideas?

Thanks for your reply,

To unsubscribe from this group and stop receiving emails from it, send an email to web...@googlegroups.com.

Paco Bernal

unread,
Nov 20, 2019, 4:39:22 PM11/20/19
to web2py-users
Hi,

Try this, write down at the begin of the controller

print request.vars

you will see how the controller receives the information and then you will know if it's a js problem or a server side problem.

with your code what the controller receives is 

<Storage {'tarea': 'solicitudguardar', 'items[json1]': '{"make":"Ford","model":"Mustang","year":1969}'}>

you don't need to use  json.loads, you just need this

mydata = request.vars['items[json1]']
print mydata
{"make":"Ford","model":"Mustang","year":1969}

Regards from spain Ugly Duckling :)

Patito Feo

unread,
Nov 20, 2019, 5:12:45 PM11/20/19
to web2py-users
Paco here is the reponse:

  1. {vars: {…}, respuesta: "noexiste"}
    1. respuesta"noexiste"


Thanks in advance,

Paco Bernal

unread,
Nov 20, 2019, 6:32:47 PM11/20/19
to web2py-users
Hiiii again :)

I think the problem is this

Instead of

request.vars.items[json1]

use this

request.vars['items[json1]']

because the var is 'items[json1]' requests.vars.items[json1] will look for the key 'json1' in the dict var items and there is no items var only a 'items[json1]' var that's why you need to use ['items[json1]'] and not items[json1]

Regards

Patito Feo

unread,
Nov 27, 2019, 8:59:07 AM11/27/19
to web2py-users
Paco, thanks for your help!

Y Olé
Reply all
Reply to author
Forward
0 new messages