0

I'm trying to receive a json object sent from my jquery post call as seen below in the code. I get the "POST OK" callback when the

simplejson.loads(request.POST) 

is commented. But as soon i'm trying do do something with the request I get Internal server error 500. Any ideas or any other ways to 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);
                } 
        });
4

1 に答える 1

2

loads()メソッドに辞書を渡そうとしているためです。request.POSTパラメータを持つ辞書です。を使用して生のコンテンツを取得できますrequest.raw_post_data

またsimplejson、Django では非推奨です。Python 2.6 以降を使用している場合は、Pythonjsonパッケージ ( import json)を使用する必要があります。

また、js コードではclient_response、json を含む param を渡します。この場合request.POST['client_response']loads()メソッドにのみ渡す必要があります。しかし、より良い方法は、json を直接渡すことです。

  data: JSON.stringify(m)
于 2013-05-15T11:56:00.117 に答える