Django ajax请求因内部服务器错误而失败

时间:2014-06-26 03:47:17

标签: jquery ajax django

我试图用Django做一个ajax请求,但是我得到了一个内部服务器错误500 ..很奇怪请求是去服务器但是当它试图返回响应时它会失败...

这是我的观点

views.py

@csrf_exempt    
def getproductinfo(request):
        to_json = {'name':'test'}
        print "Value of a = ", to_json
        try:
            json_response = simplejson.dumps(to_json)
        except (TypeError, ValueError) as err:
            print 'ERROR:', err
        print 'json_response = ', json_response
        return StreamingHttpResponse(json_response,content_type='application/json') 

ajax call

$.ajax({
        async:false
        ,url:'/getproductinfo'
        ,type: 'POST'
        ,data: {session_id: '{{request.session.sessionid}}'}
        ,success: function(msg){
            alert('Success')
        }
        ,error: function(msg){
            alert("It erroed out")
        }

    })

urls.py

url(r'^getproductinfo',getproductinfo),

请让我知道如何解决这个问题。谢谢..

1 个答案:

答案 0 :(得分:0)

您的异常处理已被破坏。如果在反序列化JSON时出错,则json_response将是未定义的。但是你在异常块之后引用它,所以在这种情况下你会得到一个你没有捕到的NameError

相关问题