如何从django HttpResponse获取JSON?

0 投票
1 回答
2219 浏览
提问于 2025-04-17 14:41

我正在尝试从Django的HttpResponse中获取json对象。其实是想访问'form_validation'这个值。但是我做不到。有什么建议吗?

这是我的HttpResponse

return HttpResponse(simplejson.dumps({'response_data':response_data, 'form_validation':form_validation, 'guest': guest, 'error_list':error_array}), mimetype="application/json")

1 个回答

0

你需要为你的ajax调用定义一个叫做success的函数,这个函数会接收到一个json格式的响应对象:

$.ajax({
        type: 'POST',
        url: url,
        data: {'csrfmiddlewaretoken': '{{csrf_token}}'},
        dataType: "text",
        success: function(response) {
            var response_data = response.response_data;
            var form_validation = response.form_validation;
            // access other fields
        },
        error: function(rs, e) {
            alert(rs.responseText);
        }
});

撰写回答