Django 如何在ajax中更改类型 template.html $(文档).ready(函数(){ var a=“{parameter}}”; $.ajax({ 类型:'GET', url:geturl(a), 数据类型:“json”, views.py 如果参数==“按天”: 打印请求方法 如果request.method==“POST”: if request.POST.get('monyrsubmit'): monthform=monthform(request.POST) 如果monthform.is_有效(): 所选月份=月份格式。已清理的月份数据[“月份”] 所选的年=月格式已清理的年数据[“年”] 每月打印所选内容 打印所选年份
我实际上是通过发送表单数据在post方法中进行操作的。但是,由于我在ajax脚本中提供了get类型,因此它有一个get请求。我正在视图中进行检查。如果request.method==“post”,但此方法仍然是get将类型更改为postDjango 如何在ajax中更改类型 template.html $(文档).ready(函数(){ var a=“{parameter}}”; $.ajax({ 类型:'GET', url:geturl(a), 数据类型:“json”, views.py 如果参数==“按天”: 打印请求方法 如果request.method==“POST”: if request.POST.get('monyrsubmit'): monthform=monthform(request.POST) 如果monthform.is_有效(): 所选月份=月份格式。已清理的月份数据[“月份”] 所选的年=月格式已清理的年数据[“年”] 每月打印所选内容 打印所选年份,django,Django,我实际上是通过发送表单数据在post方法中进行操作的。但是,由于我在ajax脚本中提供了get类型,因此它有一个get请求。我正在视图中进行检查。如果request.method==“post”,但此方法仍然是get将类型更改为post template.html <script > $(document).ready(function() { var a="{{parameter}}"; $.ajax({ type :'GET',
template.html
<script >
$(document).ready(function() {
var a="{{parameter}}";
$.ajax({
type :'GET',
url : geturl(a),
dataType : 'json',
views.py
if param=="daywise":
print request.method
if request.method=="POST":
if request.POST.get('monyrsubmit'):
monthform=MonthForm(request.POST)
if monthform.is_valid():
selected_month=monthform.cleaned_data["Month"]
selected_year=monthform.cleaned_data["Year"]
print selected_month
print selected_year
如果没有在django中禁用CSRF中间件,则会导致问题。
若要修复此问题,请将此脚本添加到
标记之后$(文档)之前。阅读…
:
var a="{{parameter}}";
$.ajax({
type :'POST',
url : geturl(a),
dataType : 'json',
$.ajaxSetup({
发送前:功能(xhr、设置){
函数getCookie(名称){
var-cookieValue=null;
if(document.cookie&&document.cookie!=''){
var cookies=document.cookie.split(“;”);
对于(变量i=0;i
有什么问题吗?可能会将类型:'GET',
更改为类型:'POST',
?我希望有一个GET和POST请求。默认情况下,当我想根据通过表单发送的wat数据检索数据时,需要将调用分开
$.ajaxSetup({
beforeSend: function(xhr, settings) {
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]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
// Only send the token to relative URLs i.e. locally.
xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
}
}
});