2013-02-19 52 views
0
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 

我其实通过发送表单数据上来POST方法。但它采取它有一个get请求,因为我已经给了类型在ajax脚本中有GET。即时进行检查的意见。如果request.method ==“POST”但这种方法仍然是GET如何改变类型的AJAX

+0

什么问题?也许改变'输入:'GET','输入':'POST','? – okm 2013-02-19 08:22:17

回答

0

更改为POST类型...

var a="{{parameter}}"; 

    $.ajax({ 
    type :'POST', 

    url : geturl(a), 

    dataType : 'json', 

如果你没有禁止在Django的CSRF中间件,就会造成问题。 修复它,在<script>标签后$(document).read...前添加此脚本:

$.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')); 
     } 
    } 
}); 
+0

我想有一个GET以及POST请求。 GET加载sumthin默认值,然后POST是当我想要检索基于我通过表格发送的数据时的数据 – prithu 2013-02-19 09:26:38

+0

您将需要单独拨打电话 – YardenST 2013-02-19 21:35:54