首页 > 解决方案 > 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?

标签: javascriptjquerydjango

解决方案


你有它以相反的顺序。它需要是:

return HttpResponse("You cannot call this method", status=400)

推荐阅读