سؤال

I have json rpc dict:

{jsonrpc: "2.0", method: "secret", params: {book: {book_dict}}, "id": 1}

this is what I submit it by ajax:

$.ajax({
    url: "RPC2",
    type:'post',
    dataType:'json',
    data : {jsonrpc: "2.0", method: "secret", params: {book: {book_dict}}, "id": 1},
    success: function( data ) {
    console.info(data)
    $( "#temp" ).html( "<strong>" + data + "</strong> degrees" );
    }
    });

the received part by below code:

re=request.REQUEST
for k in re:
    print "dict[%s] =" % k,re[k]

the received part is:

dict[id] = 1
dict[jsonrpc] = 2.0
dict[method] = secret
dict[[title]] = my writing
dict[params[author]] = me
...

I want to get the params objects:

rep = re.get('params', None)
print rep

Here I want get the objects (books), but here it just return me with None. How to get and rebuild the object dict in nested dict?

the print repr(re):

MergeDict(<QueryDict: {u'params[book][author]': [u'me'], u'params[book
][title]': [u'my writing'], u'jsonrpc': [u'2.0'], u'method': [u'secret'], u'id':
[u'1']}>, <QueryDict: {}>)

ps: I want build the whole object book: {title: "my writing", author: "me"}, but I do not know the class name and their attributes name before. so I can only use the property to build the objects.

هل كانت مفيدة؟

المحلول

If you want to post JSON, encode the JSON data structure, jQuery does not do that for you:

$.ajax({
    url: "RPC2",
    type: 'post',
    dataType: 'json',
    data : JSON.stringify({jsonrpc: "2.0", method: "secret", params: {book: {book_dict}}, "id": 1}),
    success: function( data ) {
        console.info(data);
        $( "#temp" ).html( "<strong>" + data + "</strong> degrees" );
    }
});

See Serializing to JSON in jQuery

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top