1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-03 16:10:26 +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'));
}
}

View File

@@ -287,37 +287,13 @@ $('#repo-create-submit').click(function() {
}
}
// prepare django csrf token
$.ajaxSetup({
beforeSend: function(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'));
}
}
});
$.ajax({
url: '{{ SITE_ROOT }}repo/create/',
type: 'POST',
dataType: 'json',
cache: 'false',
contentType: 'application/json; charset=utf-8',
beforeSend: prepareCSRFToken,
data: {
'repo_name': $('#repo-name').val(),
'repo_desc': $('#repo-desc').val(),

View File

@@ -135,7 +135,6 @@ $('#rm-shared-link').click(function() {
});
$('#send-shared-link').click(function() {
$('#error, #sending, #success').attr('class', 'hide');
$("#link-send-form").modal({appendTo: "#main"});
});
@@ -155,36 +154,13 @@ $("#link-send-form").submit(function(event) {
$('#simplemodal-container').css('height', $('#link-send-form').height());
if (email.length <= 512) {
// prepare django csrf token
$.ajaxSetup({
beforeSend: function(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'));
}
}
});
$.ajax({
type: "POST",
url: "{{ SITE_ROOT }}sharedlink/send/",
dataType: 'json',
cache: false,
contentType: 'application/json; charset=utf-8',
beforeSend: prepareCSRFToken,
data: {file_shared_link: file_shared_link, email: email},
success: function(data) {
$('#sending').attr('class', 'hide');