jquery - Getting internal server error when trying to handle json in django -
i'm trying receive json object sent jquery post call seen below in code. "post ok" callback when the
simplejson.loads(request.post) is commented. i'm trying do request internal server error 500. ideas or other ways handle json?
views.py
@csrf_exempt def post_post(request): print 'post_post' if request.method == 'post': print 'post' messagedata = simplejson.load(request.post) return httpresponse(simplejson.dumps("post ok!")) else: return httpresponse(simplejson.dumps("post not ok!")) projectviewmodel.js
var m = "hello world"; console.log(m); $.ajax({ url: 'postnewpost/', type: 'post', datatype: 'json', data: {client_response: json.stringify(m)}, success: function(result) { console.log(result); } });
it's because trying pass dictionary loads() method. request.post dictionary params. can raw content using request.raw_post_data.
also simplejson deprecated in django , if using python 2.6+ should use python json package (import json)
also in js code passing param client_response json in it. in case need pass request.post['client_response'] loads() method. better way pass json directly.
data: json.stringify(m)
Comments
Post a Comment