1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-03 07:55:36 +00:00

improved ajax POST

This commit is contained in:
llj
2012-07-17 17:54:05 +08:00
parent 695eb760fb
commit 0b37e40cbc
3 changed files with 25 additions and 52 deletions

View File

@@ -80,8 +80,7 @@ function filesizeformat(bytes, precision)
var gigabyte = megabyte * 1024;
var terabyte = gigabyte * 1024;
if (precision === undefined)
precision = 0;
precision = precision || 0;
if ((bytes >= 0) && (bytes < kilobyte)) {
return bytes + ' B';
@@ -106,3 +105,25 @@ function filesizeformat(bytes, precision)
function e(str) {
return encodeURIComponent(str);
}
function prepareCSRFToken(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'));
}
}