我有一个前端服务器从我的后端服务器获取一些 JSON 数据。两台服务器都运行 Django。这是获取 json 数据的确切代码..
def View(request):
r = requests.get(path)
return HttpResponse(r.json())
但是,我今天遇到了一个奇怪的问题,即在重新启动服务器后调用成功完成一次。如果我运行以下代码:-
def View(request):
r = requests.get(path)
r = requests.get(path)
return HttpResponse(r.json())
这也很成功。
但是,在第二次调用 View() 时,出现错误。错误消息是这样说的:
"uWSGI exceptions catcher for "GET /api/v1/backend/" (request plugin: "python", modifier1: 0)
Exception: TypeError: http header value must be a string
Exception class: TypeError
Exception message: http header value must be a string"
很明显,错误是在我的后端服务器上引发的,但我今天只更改了前端的一些模板。我不知道是什么导致这个问题今天开始出现。谁能指出我正确的方向?
最佳答案
给它正确的 json 头:
return HttpResponse(data, content_type='application/json')
关于python - 获取 json 请求的 Django 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21200372/