我正在尝试使用此Django代码打印与用户相关的内容:
try:
//do things
return JsonResponse({})
except Exception as e:
msg = "There was an error processing your request. " \
"Please do that. (%s)?" % e.message
return JsonResponse({'status': 'false', 'message': msg}, status=500)
这是我的javascript代码:
$.ajax({
url: '{% url "create_stats" %}',
type: 'post',
dataType: 'json',
data: $("#aform").serialize(),
success: function(data) {
window.location ="{% url "charts" %}";
},
error:function(xhr, status, error){
$("#msg-label").text('Action did not finish successfully. ' + xhr.responseText);
}
但是我得到了这个:
Action did not finish successfully. {"status": "false", "message": "There was an error processing preference authorities. Please do that. (64)?"}
答案 0 :(得分:1)
您永远不应将异常传递给客户端,因为它可能会泄漏一些敏感信息。
无论如何在开发过程中只引发一个异常,因此它会打印整个回溯,这样你就可以看到问题究竟在哪里/哪里。它不会像您期望的那样在浏览器中使用JSON,但您可以在浏览器的开发人员工具中显示原始响应。