Created
July 17, 2012 08:55
-
-
Save dougvk/3128183 to your computer and use it in GitHub Desktop.
Example JSON response to Server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if request.is_ajax(): | |
if form.is_valid(): | |
# I doubt we would ever save on an ajax POST. | |
goal = form.save() | |
# Serialize the goal in json format and send the | |
# newly created object back in the reponse | |
# have to put it in a list even if it's just one instance. | |
data = serializers.serialize('json', [goal,]) | |
else: | |
# Since form.errors is a proxy need to create a dict from it with unicode. | |
data = json.dumps(dict([(k, [unicode(e) for e in v]) for k,v in form.errors.items()])) | |
return HttpBadRequestResponse(data, mimetype='application/json') | |
return HttpResponse(data, mimetype='application/json') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment