2013-02-19 47 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