Retrieve JSON message send to Server

23 views
Skip to first unread message

Yannick

unread,
Mar 28, 2009, 4:38:25 PM3/28/09
to web2py Web Framework, mdip...@cs.depaul.edu
Hello mate,
I have a question I don't know how to retrieve a JSON message sent to
the Action from the client:

# Here is a sample of my View:
....
<script src="{{=URL(r=request,c='static/js',f='jquery-1.3.2.js')}}"
type="text/javascript"></script>
.....
.....
function Save() {

var data = $.toJSON( ....# I convert JSON object to simple text
here....));


$.ajax({

type: 'POST',

url: '{{=URL(r=request,f='action')}}',

contentType: "application/json; charset=utf-8",

data: data,

dataType: 'json'

});

}

I wanted to know How to load the value sent from the client in the
action on the server so i can process them.

I'm using FireBug i can see the request sent and the Post message in
JSON format. I think and believe everything is fine on the client side
I just don't know how to retrieve them on the server side... Since
here I don't use any argument or anything like that.

Please any help would be appreciated.

Thanks,
Yannick P.

Fran

unread,
Mar 28, 2009, 6:02:26 PM3/28/09
to web2py Web Framework
On Mar 28, 9:38 pm, Yannick <ytchatch...@gmail.com> wrote:
> URL(r=request,c='static/js',f='jquery-1.3.2.js')

Should this not be:
URL(r=request,c='static',f='js',args=['jquery-1.3.2.js'])

F

Yannick

unread,
Mar 28, 2009, 6:22:16 PM3/28/09
to web2py Web Framework
No... Like i said there is no problem from the client side... I use
this: URL(r=request,c='static/js',f='jquery-1.3.2.js') to import the
Javascript which is on the directory 'static/js'... I'm not sending
any argument to action.
Thanks Fran for the note!

Does anyone has any idea from my initial problem... Retrieving the
JSON data sent from the client...

Any hint will help !
Thanks !

DenesL

unread,
Mar 28, 2009, 11:30:51 PM3/28/09
to web2py Web Framework


On Mar 28, 3:38 pm, Yannick <ytchatch...@gmail.com> wrote:
> Hello mate,
> I have a question I don't know how to retrieve a JSON message sent to
> the Action from the client:
>
> # Here is a sample of my View:
> ....
> <script src="{{=URL(r=request,c='static/js',f='jquery-1.3.2.js')}}"
> type="text/javascript"></script>
> .....
> .....
> function Save() {
>
>                         var data = $.toJSON( ....# I convert JSON object to simple text
> here....));

First, var data has to follow these rules (from jQuery API docs):

Data to be sent to the server. It is converted to a query string, if
not already a string. It's appended to the url for GET-requests. See
processData option to prevent this automatic processing. Object must
be Key/Value pairs. If value is an Array, jQuery serializes multiple
values with same key i.e. {foo:["bar1", "bar2"]} becomes
'&foo=bar1&foo=bar2'.

>
>                         $.ajax({
>
>                                 type: 'POST',
>
>                                 url: '{{=URL(r=request,f='action')}}',
>
>                                 contentType: "application/json; charset=utf-8",
>
>                                 data: data,
>
>                                 dataType: 'json'
>
>                 });
>
>                 }
>
> I wanted to know How to load the value sent from the client in the
> action on the server so i can process them.

In your action you would check the request.vars for the keys of the
key/value pairs included in data, e.g. if

data= "id="+somevalue

then in action you obtain the value of key id as follows:

id=request.vars.id

BTW, to be useful you would probably need the callback inside your
$.ajax :

success: function(datain) { ... }

mdipierro

unread,
Mar 29, 2009, 10:05:54 AM3/29/09
to web2py Web Framework
Is it the same. URL is smart enough

Yannick

unread,
Mar 29, 2009, 11:00:37 AM3/29/09
to web2py Web Framework
Thanks a lot for the note and helps I appreciate it... I added the
callback on my ajax function... But I still have problem retrieving
JSON data...Here is the sample example of the JSON converted to string
that i pass to the Action class:
{
"Name":
[
{ "Key1": "value1",
"key2": value2,
"data": {"key": "value3"}
},
{ "Key1": "value1",
"key2": value2,
"data": {"key": "value3"}
},
]
}

I can't retrieve the value using "request.vars.Name" in my action.
The value of this variable is NONE i don't understand why... I'm
probably doing something wrong here, do you please have any idea ?

Thanks for your attention,
Yannick P.

mdipierro

unread,
Mar 29, 2009, 11:45:11 AM3/29/09
to web2py Web Framework
I think in the action you should do

import gluon.contrib.simplejson as sj
data=sj.loads(request.body.read())
# then data["Name"][0]["Key1"]=="value1"

Yannick

unread,
Mar 29, 2009, 9:29:30 PM3/29/09
to web2py Web Framework
Thanks Massimo it works fine... you made my day :)... It was
fine...Thanks this is all fun working on web2py.

Yannick

unread,
Mar 30, 2009, 10:43:04 PM3/30/09
to web2py Web Framework
Hello mate
I'm trying to parse the JSON object inside the callback method from my
client side, I got a message saying "No JSON object could be
decoded"... Do you please have any idea about what I'm doing wrong in
my callback method...Below you can see the sample of the callback
method, and format of JSON message... thanks

# from the client side I return a JSON that i want to parse:
function Save() {
var data = $.toJSON( ....# I convert JSON object to simple text
here....));
$.ajax({
type: 'POST',
url: '{{=URL(r=request,f='action')}}',
contentType: "application/json;
charset=utf-8",
data: data,
dataType: 'json'
success: function(data){ {{

import gluon.contrib.simplejson as sj

data = sj.loads(response.body.read())
value = data["Name"][0]["key1"]
}}
alert({{=value}}); }

});
});

}

Here is the format of the JSON that is sent as a response from my
action client (I can see it from Firebug debuger)

{
"Name":
[
{ "Key1": "value1",
"key2": value2,
"data": {"key": "value3"}
},
{ "Key1": "value1",
"key2": value2,
"data": {"key": "value3"}
},
]

}

Any ideas ?

Yannick P.

Do you know the
On Mar 29, 11:45 am, mdipierro <mdipie...@cs.depaul.edu> wrote:
Reply all
Reply to author
Forward
0 new messages