1

I am trying to send a json object from javascript to a python webservice. But the service always treats it as a string. Below are the client and server side codes:

CLIENT SIDE:

$("#button").click(function () {
        $.ajax({
            type: "POST",
            url: "http://localhost:8079/add",
            data: JSON.stringify([{'account_template': {
                    'external_id': 'l10n_harec.a' + $("input[id$=Text2]").val(),
                    'name': $("input[id$=Text1]").val(),
                    'code': $("input[id$=Text2]").val(),
                    'type': $("select[id$=accountType]").val(),
                    'reconcile': $("input[id$=Checkbox1]").val()
                }, 'account_account': {
                    'code': $("input[id$=Text2]").val(),
                    'name': $("input[id$=Text1]").val(),
                    'type': $("select[id$=accountType]").val(),
                    'active': 'True',
                    'reconcile': $("input[id$=Checkbox1]").val()
                }
            }]),
            dataType: "json",
        });
    });

SERVER SIDE:

class add:        
    def POST(self):
        i = web.input()
        print i

I can see the following on server side as a result:

Can anyone tel what is wrong here?

1
  • try to print this simplejson.loads(request.body). Commented Dec 3, 2013 at 13:35

1 Answer 1

1

I don't know what module you're using, but i expect it to always be passed as a string. If you expect a dictionary, you can use json.loads to do that:

import json
i = json.loads(web.data())
print type(i)
Sign up to request clarification or add additional context in comments.

5 Comments

Oh sorry haha, i forgot to call the function. Updated.
json.loads should take care of de-serializing any valid json, not only object -> dictionary.
I am getting TypeError: expected string or buffer when using the above method
<Storage {'json': u'{"account_template":{"external_id":"l10n_harec.a324324","name":"dsfasd","code":"324324","type":"payable","reconcile":"FALSE"},"account_account":{"code":"324324","name":"dsfasd","type":"payable","active":"True","reconcile":"FALSE"}}'}>
web.data() did it for me: s = web.data() d = json.loads(s) print d

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.