首页 > django如何获得post过来的json格式的数据

django如何获得post过来的json格式的数据

客户端代码:

def http_post(values):
    json_data = json.dumps(values)
    try:
        req = urllib2.Request(post_server,json_data)   #生成页面请求的完整数据
        response = urllib2.urlopen(req)    # 发送页面请求
    except urllib2.HTTPError,error:
        print "ERROR: ",error.read()

Django端代码:

def recv_data(request):
    if request.method == 'POST':
        received_json_data = json.loads(request.body)
        return received_json_data
    else:
        print 'abc'

程序运行后,client端总是报错:
You're seeing this error because you have DEBUG = True in your
Django settings file. Change that to False, and Django will
display a standard 500 page.

  请问是什么原因啊?!

他说的很明白: 在你的设置文件中你把 'DEBUG = True' 这个 DEBUG 的值改为 false就好了


500django程序执行的时候错误。

目测你POST的数据不是标准的json字符串loads的时候报错

你可以打印一下 request.body

设置 Debug=True

可以看到更详细的报错信息。


解决了了,是json的问题!

def recv_data(request):
    if request.method == 'POST':
        req = json.loads(request.body)
【热门文章】
【热门文章】