0
$.ajax({
    type :'GET',

    url : geturl(a),
    // type: $(this).attr('method'),
    dataType : 'json',

视图.py:

  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  

我可以在 ajax 的类型字段中有 GET 和 POST 请求吗?我使用表单并且仅当单击提交按钮时我试图根据提交的数据显示信息。如果 request.POST.get('monyrsubmit') 不起作用。帮助将不胜感激

4

1 回答 1

0

这很简单。您必须抽象事件。

function event_page_load() {
   function ajax_request('GET')
}

function click_submit_button() {
   function ajax_request('POST')
}

function ajax_request(type) {

 $.ajax({
    type : type,
    ......
    ......
 })
}

您还可以考虑以下一般准则。应根据对服务器的请求类型使用 GET 和 POST

 - If you are reading the existing data(without modification) from the server, use GET
 - if you are writing/modifying any data in the server, use POST

在 jQuery 中,您可以使用这些简单的方法。

对于 GET 请求

$.get(  
    url,  
    {param1: "value1", param2: "value2"},  
    function(responseText){  
        // todo ;  
    },  
    "html"  
);  

对于 POST 请求

$.post(  
    url,  
    {param1: "value1", param2: "value2"},  
    function(responseText){  
        // todo ;  
    },  
    "html"  
);  

确保您已禁用浏览器缓存。

$.ajaxSetup ({  
        cache: false  
    });

在 django 端,您可以使用request.is_ajax()方法来验证 ajax 调用,并且可以基于request.method属性进行过滤。

您可以在https://github.com/sivaa/django-jquery-ajax-exmaples参考 Djano 的所有可能用法

于 2013-02-19T10:37:22.873 回答