python - How to do JSON handler in Django -
i want , parse json in django view.
requst in template:
var values = {}; $("input[name^='param']").each(function() { values[$(this).attr("name")] = $(this).val(); }); $.ajax ({ type: "post", url: page, contenttype: 'application/json; charset=utf-8', async: false, processdata: false, data: $.tojson(values), success: function (resp) { console.log(resp); } });
in view:
import json ... req = json.loads(request.body) return httpresponse(req)
it give me error:
the json object must str, not 'bytes'
what wrong?
most web framework consider string representation utf-8, bytes in python 3 (like django, , pyramid). in python3 needs decode('utf-8') body in:
req = json.loads( request.body.decode('utf-8') )
Comments
Post a Comment