Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django jquery ajax 403 error

I am trying to get ajax to work, but I keep getting a 403 error. I am quite new to jquery.

The following is my code

    $('#prod_search_button').click(function(){
    if ($('#inv_prod_list').length) {
        //insert a new record
    }
    else
    {
        //create the #inv_prod_list table and insert first record
        var inv_table= '<table id="inv_prod_list" style="border: 2px solid #dddddd;"></table>';

        // create query object
        var prod_query = {
            query: jQuery.trim($('#id_prod_query').val())
        };

        // convert object to JSON data
        var jsonQuery = JSON.stringify(prod_query);

        $.ajax({
            type: 'POST',
            url: '/company/product/item_search.json/',
            data: jsonQuery,

             success: function(jsonData){
                    var parsed = JSON.parse(jsonData);
                    $('#inv_prod_wrap').html(inv_table);

                    var new_record = 'this is html for new row'

                    $('#inv_prod_list tr:last').after(new_record);



                    //off rows alt color
                   }
        });
    }
});
like image 518
bash- Avatar asked Oct 04 '11 10:10

bash-


2 Answers

You can avoid the CSRF by adding the following annotation before your method definition.

from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def Method():
like image 61
Geoffrey-ap Avatar answered Nov 07 '22 02:11

Geoffrey-ap


I think you don't pass CSRF token.

like image 35
DrTyrsa Avatar answered Nov 07 '22 01:11

DrTyrsa