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:
@@ -80,8 +80,7 @@ function filesizeformat(bytes, precision)
|
|||||||
var gigabyte = megabyte * 1024;
|
var gigabyte = megabyte * 1024;
|
||||||
var terabyte = gigabyte * 1024;
|
var terabyte = gigabyte * 1024;
|
||||||
|
|
||||||
if (precision === undefined)
|
precision = precision || 0;
|
||||||
precision = 0;
|
|
||||||
|
|
||||||
if ((bytes >= 0) && (bytes < kilobyte)) {
|
if ((bytes >= 0) && (bytes < kilobyte)) {
|
||||||
return bytes + ' B';
|
return bytes + ' B';
|
||||||
@@ -106,3 +105,25 @@ function filesizeformat(bytes, precision)
|
|||||||
function e(str) {
|
function e(str) {
|
||||||
return encodeURIComponent(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'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@@ -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({
|
$.ajax({
|
||||||
url: '{{ SITE_ROOT }}repo/create/',
|
url: '{{ SITE_ROOT }}repo/create/',
|
||||||
type: 'POST',
|
type: 'POST',
|
||||||
dataType: 'json',
|
dataType: 'json',
|
||||||
cache: 'false',
|
cache: 'false',
|
||||||
contentType: 'application/json; charset=utf-8',
|
contentType: 'application/json; charset=utf-8',
|
||||||
|
beforeSend: prepareCSRFToken,
|
||||||
data: {
|
data: {
|
||||||
'repo_name': $('#repo-name').val(),
|
'repo_name': $('#repo-name').val(),
|
||||||
'repo_desc': $('#repo-desc').val(),
|
'repo_desc': $('#repo-desc').val(),
|
||||||
|
@@ -135,7 +135,6 @@ $('#rm-shared-link').click(function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
$('#send-shared-link').click(function() {
|
$('#send-shared-link').click(function() {
|
||||||
$('#error, #sending, #success').attr('class', 'hide');
|
|
||||||
$("#link-send-form").modal({appendTo: "#main"});
|
$("#link-send-form").modal({appendTo: "#main"});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -155,36 +154,13 @@ $("#link-send-form").submit(function(event) {
|
|||||||
$('#simplemodal-container').css('height', $('#link-send-form').height());
|
$('#simplemodal-container').css('height', $('#link-send-form').height());
|
||||||
|
|
||||||
if (email.length <= 512) {
|
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({
|
$.ajax({
|
||||||
type: "POST",
|
type: "POST",
|
||||||
url: "{{ SITE_ROOT }}sharedlink/send/",
|
url: "{{ SITE_ROOT }}sharedlink/send/",
|
||||||
dataType: 'json',
|
dataType: 'json',
|
||||||
cache: false,
|
cache: false,
|
||||||
contentType: 'application/json; charset=utf-8',
|
contentType: 'application/json; charset=utf-8',
|
||||||
|
beforeSend: prepareCSRFToken,
|
||||||
data: {file_shared_link: file_shared_link, email: email},
|
data: {file_shared_link: file_shared_link, email: email},
|
||||||
success: function(data) {
|
success: function(data) {
|
||||||
$('#sending').attr('class', 'hide');
|
$('#sending').attr('class', 'hide');
|
||||||
|
Reference in New Issue
Block a user