因此,我在删除功能方面遇到问题,它确实删除了它想要删除的对象,但不会转到window.location。相反,我得到了错误

不存在于/ api / personnel / delete /
资源匹配查询不存在。

我想是因为它刚被删除。我如何解决这个问题?

var deletepers = function(){
var persid = getUrlVars()["id"];
data={persid}
console.log(persid);
        $.ajax({
        type: "POST",
        url: "/api/personnel/delete/",
        data: JSON.stringify(data),
        contentType: "application/json",
        dataType: 'json',
            success:function(response){
                window.location.href = "/Personnel";
        }
    })
}

def delete_personnel(request):

    # Try find the requested app
    if request.method == "POST":
    pers_id = request.POST.get('persid')

    pers = Resource.objects.get(id=pers_id)
    if not pers: return HttpResponseNotFound()

    pers.delete()

    return HttpResponse(content_type='application/json')

最佳答案

您没有按照视图期望的格式传递数据。 JS中的{persid}解释为persid,根本不是哈希。因此在视图中,request.POST.get('persid')为无。

而是使用实际的JS哈希:

data = { persid: persid }

07-24 16:08