Django的/ AJAX:无法获取阿贾克斯后的数据在views.py [英] django/ajax: Unable to get Ajax post data in the views.py
问题描述
我有一个问题,从Django的后端越来越阿贾克斯后的数据,我不知道该如何传递值,请大家帮忙。
I had a problem getting ajax post data from django backend, I don't know how to pass the value, please help.
在HTML我只是这样的:
In html I have simply this:
<form id="get_vulns_from_family">
<label for="family_content">Enter a family name to display the NVTs</label>
<input id="family_content" />
<input type="submit" value="search" />
</form>
在JavaScript的我写的:
In javascript I wrote this:
$(function() {
$("#get_vulns_from_family").submit(function(event) {
var family_text = $("#family_content").val();
var family_data = {"family": family_text};
$.ajax({
url: "/template_conf/get_vulns_from_family",
type: "POST",
data: family_data,
success: function(response) {
console.log(response);
},
error: function(response) {
console.log("failed!");
}
});
// prevent default posting of form
event.preventDefault();
});
});
在对应的URL Django的方法 / template_conf / get_vulns_from_family
,我想这样的:
In Django method corresponding to url /template_conf/get_vulns_from_family
, I tried this:
def get_vuln_from_family(request):
family = request.POST['family']
# some other operations to get value for variable "json_data"
return HttpResponse(simplejson.dumps(json_data))
不过在Django说: MultiValueDictKeyError:的QueryDict钥匙'家庭'未与&lt发现:{}&GT;
,这意味着POST字典是空的。
我使用了错误的方式来获得数据后?如果是的话我该怎么办?谢谢你。
But django said: MultiValueDictKeyError: "Key 'family' not found in <QueryDict: {}>"
, which means the POST dictionary is empty.
Am I using the wrong way to get post data? If so what should I do? Thanks.
推荐答案
您的网址/ template_conf / get_vulns_from_family
缺少最后的斜线。 Django会通常重定向为/ template_conf / get_vulns_from_family /
,下探 POST
数据
your url "/template_conf/get_vulns_from_family"
is missing a trailing slash. django will typically redirect this to "/template_conf/get_vulns_from_family/"
, dropping the POST
data
这篇关于Django的/ AJAX:无法获取阿贾克斯后的数据在views.py的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!