我从django后端获取ajax发布数据时遇到问题,我不知道如何传递该值,请帮忙.
在HTML中我只是这个:
<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>
Run Code Online (Sandbox Code Playgroud)
在javascript中我写了这个:
$(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();
});
});
Run Code Online (Sandbox Code Playgroud)
在与url对应的Django方法中/template_conf/get_vulns_from_family,我试过这个:
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))
Run Code Online (Sandbox Code Playgroud)
但是django说:MultiValueDictKeyError: "Key 'family' not found in <QueryDict: {}>"这意味着POST字典是空的.我使用错误的方式获取发布数据吗?如果是这样我该怎么办?谢谢.
sec*_*ond 10
你的网址"/template_conf/get_vulns_from_family"缺少一个斜杠.django通常会将此重定向到"/template_conf/get_vulns_from_family/"丢弃POST数据
| 归档时间: |
|
| 查看次数: |
4407 次 |
| 最近记录: |