Django / ajax: unable to get Ajax post data in views.py file

I had a problem getting ajax post data from the django backend, I don’t know how to pass the value, please help.

In html, I just have the following:

<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> 

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(); }); }); 

In the 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)) 

But django said: MultiValueDictKeyError: "Key 'family' not found in <QueryDict: {}>" , which means the POST dictionary is empty. Am I using the wrong way to retrieve data for publication? If so, what should I do? Thanks.

+4
source share
2 answers

your url "/template_conf/get_vulns_from_family" missing a "/template_conf/get_vulns_from_family" slash. django usually redirects this to "/template_conf/get_vulns_from_family/" , discarding the POST data

+10
source

If your CSRF is enabled, then a simple ajax message does not work. you will need to add the csrf token and set it to the ajax request header.

For an Ajax POJ request, you need to pass the CSRF token as POST data with each POST request. For this reason, you must first get the CSRF token. Since you have enabled CSRF protection to get the token from the csrftoken cookie. By default, the CSRF cookie is called csrftoken. Purchasing a marker is very straightforward and can be achieved using the code snippet below.

 function getCookie(name) { var cookieValue = null; if (document.cookie && document.cookie != '') { var cookies = document.cookie.split(';'); for (var i = 0; i < cookies.length; i++) { var cookie = jQuery.trim(cookies[i]); if (cookie.substring(0, name.length + 1) == (name + '=')) { cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); break; } } } return cookieValue; } function csrfSafeMethod(method) { return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method)); } function sameOrigin(url) { var host = document.location.host; // host + port var protocol = document.location.protocol; var sr_origin = '//' + host; var origin = protocol + sr_origin; return (url == origin || url.slice(0, origin.length + 1) == origin + '/') || (url == sr_origin || url.slice(0, sr_origin.length + 1) == sr_origin + '/') || !(/^(\/\/|http:|https:).*/.test(url)); } $(function() { $("#person_form_id").submit(function(event){ event.preventDefault(); $.ajax({ type:$(this).attr('method'), url:"", data:$(this).serialize(), success: function(){ $('#message').html("<h2 style='color:green;'>Person Form Submitted!</h2>") }, error: function(){ $('#message').html("<h2 style='color:red;'>Can't submit form</h2>") } }); return false; }); }); $.ajaxSetup({ beforeSend: function(xhr, settings) { if (!csrfSafeMethod(settings.type) && sameOrigin(settings.url)) { // Send the token to same-origin, relative URLs only. // Send the token only if the method warrants CSRF protection // Using the CSRFToken value acquired earlier xhr.setRequestHeader("X-CSRFToken", csrftoken); } } }); 
0
source

All Articles