简体   繁体   English

无法解码Python中Javascript的JSON响应

[英]Can't decode JSON response from Javascript in Python

I'm trying to send a simple message using JQuery.post() , but it gives this error on Python : 我正在尝试使用JQuery.post()发送一条简单的消息,但它在Python上给出了这个错误:

JSONDecodeError at cadastro/editPessoa/ cadastro / editPessoa /的JSONDecodeError

Expecting value: line 1 column 1 (char 0) 期望值:第1行第1列(char 0)

Javascript (simplificated) : Javascript(简化):

data = { "pessoa" : [ ["1","joão","321","camarão"] ] }
$.post('editPessoa/', data, function(response) {
    updatePessoas();
  }, 'json');

views.py : views.py:

@csrf_exempt    
def editPessoa(request):
    data = json.loads(request.body.decode('utf-8'))
    print(data)
    return HttpResponse('Done!')

The error is raised while trying to json.loads(), if I remove the .decode() the error happens on the print(), it raises that bytes can't be printed 尝试json.loads()时出错,如果我删除.decode() ,print()上发生错误,它会引发无法打印的字节

Traceback : 追溯 :

JSONDecodeError at /cadastro/editPessoa/
Expecting value: line 1 column 1 (char 0)

Request Method: POST
Request URL: http://localhost:8000/cadastro/editPessoa/
Django Version: 1.10.3
Python Executable: C:\Users\6036794\Documents\django\djangocadastro\py35\Scripts\python.exe
Python Version: 3.5.2
Python Path: ['c:\\Users\\6036794\\Documents\\django\\djangocadastro', 'C:\\Python27', 'C:\\Users\\6036794
\\Documents\\django\\djangocadastro\\py35\\Scripts\\python35.zip', 'C:\\Python35\\DLLs', 'C:\\Python35
\\lib', 'C:\\Python35', 'C:\\Users\\6036794\\Documents\\django\\djangocadastro\\py35', 'C:\\Users\\6036794
\\Documents\\django\\djangocadastro\\py35\\lib\\site-packages']
Server time: Sex, 18 Nov 2016 15:33:35 -0200
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'cadastroapp']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']


Traceback:  

File "C:\Python35\lib\json\decoder.py" in raw_decode
  355.             obj, end = self.scan_once(s, idx)


      During handling of the above exception (0), another exception occurred:



File "C:\Users\6036794\Documents\django\djangocadastro\py35\lib\site-packages\django\core\handlers\exception
.py" in inner
  39.             response = get_response(request)

File "C:\Users\6036794\Documents\django\djangocadastro\py35\lib\site-packages\django\core\handlers\base
.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "C:\Users\6036794\Documents\django\djangocadastro\py35\lib\site-packages\django\core\handlers\base
.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\6036794\Documents\django\djangocadastro\py35\lib\site-packages\django\views\decorators
\csrf.py" in wrapped_view
  58.         return view_func(*args, **kwargs)

File "c:\Users\6036794\Documents\django\djangocadastro\cadastroapp\views.py" in editPessoa
  19.   data = json.loads(request.body.decode('utf-8'))

File "C:\Python35\lib\json\__init__.py" in loads
  319.         return _default_decoder.decode(s)

File "C:\Python35\lib\json\decoder.py" in decode
  339.         obj, end = self.raw_decode(s, idx=_w(s, 0).end())

File "C:\Python35\lib\json\decoder.py" in raw_decode
  357.             raise JSONDecodeError("Expecting value", s, err.value) from None

Exception Type: JSONDecodeError at /cadastro/editPessoa/
Exception Value: Expecting value: line 1 column 1 (char 0)
Request information:
USER: 6036794

GET: No GET data

POST:
pessoa[0][] = 'Camarão'

FILES: No FILES data

The problem is that the data needed to be stringified first : 问题是数据需要首先进行字符串化:

data = { "pessoa" : [ ["1","joão","321","camarão"] ] }
$.post('editPessoa/', JSON.stringify(data), function(response) {
    updatePessoas();
  }, 'json');

I assumed that it was automatically done by $.post() 我以为它是由$.post()自动完成的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM