add Ox.Cookie, send X-CSRFToken if csrftoken cookie is set

This commit is contained in:
j 2013-03-16 17:24:43 +05:30
parent 0ba564c104
commit 852641335d
2 changed files with 26 additions and 0 deletions

View file

@ -0,0 +1,20 @@
Ox.Cookies = function() {
var name, value, cookies;
if (arguments.length == 1) {
name = arguments[0];
return Ox.Cookies()[name];
} else if (arguments.length == 2) {
name = arguments[0];
value = arguments[1];
document.cookie = name + '=' + encodeURIComponent(value);
} else {
value = {}
if (document.cookie && document.cookie != '') {
document.cookie.split('; ').forEach(function(cookie) {
name = cookie.split('=')[0];
value[name] = decodeURIComponent(cookie.substring(name.length + 1));
});
}
return value;
}
}

View file

@ -136,6 +136,12 @@ Ox.Request = (function() {
} else {
pending[options.id] = true;
$.ajax({
beforeSend: function (request) {
var csrftoken = Ox.Cookies('csrftoken');
if (csrftoken) {
request.setRequestHeader("X-CSRFToken", csrftoken);
}
},
complete: complete,
data: options.data,
//dataType: 'json',