JSON解码错误:期望值:第1行第1列(字符0)

时间:2019-11-02 05:28:39

标签: json django api

我正在使用Django运行管理网站。尝试登录我的管理站点,我收到JSON解码错误。我得到的响应是404服务器错误。谁能帮助摆脱这个困境?

这是我的views.py:

def user_login(request):
datas= {'log':False}
if request.method == "POST":
    usern=request.POST.get('Username')
    print(usern)
    passw=request.POST.get('password')
    print(passw)
    response = requests.post(url='http://www.onebookingsystem.com/productionApi/API/Admin/login.php',data={"Username":usern,"password":passw})
    print(response)
    json_data = response.json()
    print(json_data)
    if json_data['status'] == 1:
        user=authenticate(Username=usern,password=passw)
        login(request,user)
        range_yearly = 0
        range_monthly = 0
        respo = requests.get(url='http://www.onebookingsystem.com/productionApi/API/Admin/admin_dashboard.php')
        data_dash = json.loads(respo.text)

我得到的错误是:

<Response [404]>
Internal Server Error: /
Traceback (most recent call last):
  File "C:\Users\Android V\Anaconda3\envs\djangoenv\lib\site-packages\django\cor
e\handlers\exception.py", line 34, in inner
    response = get_response(request)
File "C:\Users\Android V\Anaconda3\envs\djangoenv\lib\site-packages\django\cor
e\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\Android V\Anaconda3\envs\djangoenv\lib\site-packages\django\cor
e\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Android V\admin\obs_app\views.py", line 20, in user_login
    json_data = response.json()
  File "C:\Users\Android V\Anaconda3\envs\djangoenv\lib\site-packages\requests\m
odels.py", line 897, in json
    return complexjson.loads(self.text, **kwargs)
  File "C:\Users\Android V\Anaconda3\envs\djangoenv\lib\json\__init__.py", line
354, in loads
    return _default_decoder.decode(s)
  File "C:\Users\Android V\Anaconda3\envs\djangoenv\lib\json\decoder.py", line 3
39, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\Android V\Anaconda3\envs\djangoenv\lib\json\decoder.py", line 3
57, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
[02/Nov/2019 10:51:12] "POST / HTTP/1.1" 500 89429

1 个答案:

答案 0 :(得分:2)

我认为它可以帮助您: 首先导入json,它是python内置库之一。 然后使用此库,使用以下语法将请求的响应转换为json对象:

    import json

    json_data = json.loads(response)