javascript - Django返回触发jquery函数中的错误视图
问题描述
我在 django 中有以下视图:
def update_card(request):
return HttpResponse(status=400, message="You cannot call this method").
在 jquery 中:
$.ajax({
url: "{% url 'update_card' %}",
type: 'POST',
success: function(data){
console.log('OK!')
},
error: function(err){
var response_message = err.responseText;
alert(err.responseText)
}
});
但无论出于何种原因,这都会触发该success
方法,除非我执行类似raise Exception(200, "You cannot call this method")
. 为什么?我是否不允许将 HttpResponse 对象返回给 jquery?
解决方案
你有它以相反的顺序。它需要是:
return HttpResponse("You cannot call this method", status=400)
推荐阅读
- javascript - karma-typescript 被 .d.ts 打字混淆为无法绑定到 npm 包代码
- ios - 如何在 iOS 中编写间隙类型自定义 uiprogressview?
- android - 在带有重定向的 Android Studio 上显示 Internet 连接对话框
- powerbi - 使用DAX在power BI中没有所选月份和所选国家/地区的数据时如何从切片器显示所选国家/地区名称
- pytorch - 在多节点多 GPU 上使用 MPI 分发的 PyTorch
- c# - Unity 中带有“CanvasGroup.interactable”的“MissingComponentException”
- angular - 匿名登录后无法访问 Angular 上的 Firestore 数据
- javascript - Javascript 输出结果,间隔一秒
- azure-iot-hub - Azure IoT Edge 设备管理的访问控制
- amazon-web-services - 无法 ssh 到自定义 vpc 中公有子网内的 EC2 实例