我正在尝试使用此 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)?"}
切勿将异常传递给客户端,因为它可能会泄漏一些敏感信息。
无论如何,在开发过程中只需引发异常,因此它将打印整个回溯,以便您可以看到问题的确切内容/位置的上下文。它不会像您期望的那样在浏览器中使用 JSON,但您可以在浏览器的开发人员工具中显示原始响应。