我是Django的新手,我正在尝试创建一个简单的网页,当单击按钮时,该网页会发送ajax请求。但是帖子请求没有发送 模板
<form action="" method="post" id="post-form" enctype="multipart/form-data">
{% csrf_token %}
<select id="select_dropdown">
<option value="joshua">joshua</option>
<option value="peter">peter</option>
</select>
<input type="button" method = "POST" value="Apply" id="submitme" name="submitme">
</form>.
<script src="{% static 'js/jquery-3.4.1.js' %}"></script>
<script src="{% static 'js/bootstrap.js' %}"></script>
{% csrf_token %}
<script language="JavaScript">
$( document ).ready(function() {
$('#submitme').click(function(e){
e.preventDefault();
var p = document.getElementById("select_dropdown");
var value = p.options[e.selectedIndex].value;
$.ajax({
url: window.location.href,
type: "post", // or "get"
data: value,
headers: {'X-CSRFToken': '{{ csrf_token }}'}, // for csrf token
success: function(data) {
window.location.replace("/video_courses/");
alert(data.result);
}});
});
});
</script>
view.py
if request.method == "POST": # os request.GET()
print("inside post")
get_value = request.body
data = {}
data['result'] = 'you made a request'
return HttpResponse(json.dumps(data), content_type="application/json")
请帮助