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).
    – Kousik
    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)
5
  • Oh sorry haha, i forgot to call the function. Updated.
    – aIKid
    Commented Dec 3, 2013 at 13:42
  • json.loads should take care of de-serializing any valid json, not only object -> dictionary.
    – bcollins
    Commented Dec 3, 2013 at 14:05
  • I am getting TypeError: expected string or buffer when using the above method Commented Dec 4, 2013 at 6:10
  • <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"}}'}> Commented Dec 4, 2013 at 7:38
  • web.data() did it for me: s = web.data() d = json.loads(s) print d Commented Dec 4, 2013 at 10:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.