内部服务器eroor 500 django当使用simplejson.loads(request.raw_post_data) [英] internal server eroor 500 django when using simplejson.loads(request.raw_post_data)

查看:273
本文介绍了内部服务器eroor 500 django当使用simplejson.loads(request.raw_post_data)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的jquery客户端接收一个json对象,但是当我调用simplejson.loads(request.raw_post_data)时,我收到内部服务器错误500.服务器接收到调用,因为我收到回调当simplejson.loads(request.raw_post_data)行被注释时,服务器。但是一旦我取消了该行的注释,就会抛出错误。



ajaxtest.html

 code><!DOCTYPE html> 
< html>
< head>
< script src =// ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js\"></script>
< script type =text / javascript>
$(document).ready(function(){
$(#post)。click(function(){
var test = new Object();
test .name =Kalle;
test.pw =kula;
$ .ajax({
url:ajaxtest /,
type:'POST',
dataType:'json',
data:{client_response:JSON.stringify(test),csrfmiddlewaretoken:'{{csrf_token}}'},
success:function(result){
alert(result);
}
});
});
});
< / script>



views.py

从django.shortcuts导入render 
从django.shortcuts导入render_to_response
从django.template import RequestContext
从django.http导入HttpResponse
从django.utils import simplejson
从manges.models import用户

#在这里创建您的意见。

def ajax_test(request):
print'Hej'
if request.method =='GET':
print'GET'
c = User .objects.get(pk = 1)
打印c.emailadress
打印c.password
返回HttpResponse(simplejson.dumps(c,default = User.serializable))
if request.method =='POST':
print'POST'
data = simplejson.load(request.raw_post_data)
print json_data.name
return HttpResponse(simplejson.dumps 'b
$ b''''''
打印'开始'
返回render_to_response('ajaxtest.html',context_instance = RequestContext(request))

urls.py

 code> from django.conf.urls import pattern,include,url 
from manges.views import ajax_test,start_page
from django.contrib import admin

admin.autodiscover ()

urlpatterns = patterns('manges.views',
url(r'^ $',start_page),
ur l(r'^ ajaxtest / $',ajax_test),
#示例:
#url(r'^ $','myforum.views.home',name ='home'),
#url(r'^ blog /',include('blog.urls')),

url(r'^ admin / doc /',include('django.contrib.admindocs。 urls')),
url(r'^ admin /',include(admin.site.urls)),

任何想法?

解决方案

raw_post_data不包含JSON,为什么转换是投掷的。



发送请求时,您传递了这个数据

 code> {client_response:JSON.stringify(test),csrfmiddlewaretoken:'{{csrf_token}}'} 

JQuery在POST参数中序列化该对象。请参阅 jQuery.ajax 页面。



所以在服务器端,HttpRequest.raw_post_data没有直接的JSON。要提取JSON,您需要使用包含JSON的POST参数,可以通过

  request.POST [client_response]访问


I'm trying to receive a json object from my jquery client but when I call the simplejson.loads(request.raw_post_data) I get internal server error 500. The server receives the call since I'm getting the callback fro the server when the simplejson.loads(request.raw_post_data) line is commented. But as soon as I uncomment that line the error is thrown.

ajaxtest.html

    <!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
            $("#post").click(function(){
            var test = new Object();
            test.name = "Kalle";
            test.pw = "kula";
            $.ajax({
                url: "ajaxtest/",
                type: 'POST',
                dataType: 'json',
                data: {client_response: JSON.stringify(test), csrfmiddlewaretoken: '{{  csrf_token}}'},
                success: function(result) {
                    alert(result);
                } 
            });
        });      
    });
</script>

views.py

from django.shortcuts import render
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponse
from django.utils import simplejson
from manges.models import User

# Create your views here.

def ajax_test(request):
print 'Hej'
if request.method == 'GET':
    print 'GET'
    c = User.objects.get(pk=1)
    print c.emailadress
    print c.password
    return HttpResponse(simplejson.dumps(c, default=User.serializable))
if request.method == 'POST':
    print 'POST'
    data = simplejson.load(request.raw_post_data)
    print json_data.name
    return HttpResponse(simplejson.dumps('Got JSON!'))      

def start_page(request):
print 'Start'
return render_to_response('ajaxtest.html',      context_instance=RequestContext(request))

urls.py

from django.conf.urls import patterns, include, url
from manges.views import ajax_test, start_page
from django.contrib import admin

admin.autodiscover()

urlpatterns = patterns('manges.views',
url(r'^$', start_page),
url(r'^ajaxtest/$', ajax_test),
# Examples:
# url(r'^$', 'myforum.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),

url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),  
)

Any ideas??

解决方案

raw_post_data doesn't contain JSON, which is why the conversion is throwing.

When you send the request, you passed this data

{client_response: JSON.stringify(test), csrfmiddlewaretoken: '{{  csrf_token}}'}

Jquery serializes that object in POST parameters. See the page for jQuery.ajax.

So on the server side, HttpRequest.raw_post_data doesn't have JSON directly. To extract the JSON, you need to use the POST parameter that contains JSON, accessible through

request.POST["client_response"]

这篇关于内部服务器eroor 500 django当使用simplejson.loads(request.raw_post_data)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆