Skip to content
yeealex93 edited this page Aug 15, 2012 · 13 revisions

To make a script that sends a valid cookie for an ajax request, include ajaxCSRFfix.js in your script. This will append a valid cookie to each ajax request. This should only be required for certain types of ajax requests, such as POST.

location: static/lib/ajaxCSRFfix.js

Ajax requests can be done using JQuery. For example:

function (param1, ...) {
    var all_my_vars = ...;
    non_local_vars = ...;

    jQuery.ajax({
        url: '/your/url/',
        type: 'POST',
        data: {'vars' : all_my_vars}, //dictionary of data to pass 
        success: function(response) {
            //param1 and all_my_vars is NOT in scope here.
            //non_local_vars IS in scope here.
            successfunction(); 
        },
        failure: function(response) {
            //param1 and all_my_vars are NOT in scope here.
            //non_local_vars IS in scope here.
            failurefunction();
        }
    });

    successfunction = function () {
        //do something on success
        //all_my_vars is NOT in scope here.
        //param1 and non_local_vars ARE in scope here.
    };
    failurefunction = function () {
        //do something on failure
        //all_my_vars is NOT in scope here.
        //param1 and non_local_vars ARE in scope here.
    };
};
Clone this wiki locally