From 83aaef0f824e8776ddf4c60a44e6d56c21a0efcc Mon Sep 17 00:00:00 2001 From: Pitu Date: Thu, 19 Jan 2017 02:37:35 -0300 Subject: Changed request system and post data Changed from XMLHttpRequest to Axiios and made every POST call to look for params or json and not pass the values as headers. Token is still a header though --- public/js/upload.js | 83 +++++++++++++++++++++++++++-------------------------- 1 file changed, 42 insertions(+), 41 deletions(-) (limited to 'public/js/upload.js') diff --git a/public/js/upload.js b/public/js/upload.js index d6d9b9d..35208f3 100644 --- a/public/js/upload.js +++ b/public/js/upload.js @@ -5,16 +5,18 @@ upload.token = localStorage.token; upload.maxFileSize; upload.checkIfPublic = function(){ - var xhr = new XMLHttpRequest(); - xhr.onreadystatechange = function() { - if (xhr.readyState == XMLHttpRequest.DONE) { - upload.isPublic = JSON.parse(xhr.responseText).private; - upload.maxFileSize = JSON.parse(xhr.responseText).maxFileSize; - upload.preparePage(); - } - } - xhr.open('GET', '/api/check', true); - xhr.send(null); + + axios.get('/api/check') + .then(function (response) { + upload.isPublic = response.data.private; + upload.maxFileSize = response.data.maxFileSize; + upload.preparePage(); + }) + .catch(function (error) { + return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error"); + console.log(error); + }); + } upload.preparePage = function(){ @@ -30,38 +32,37 @@ upload.preparePage = function(){ } upload.verifyToken = function(token, reloadOnError = false){ - var xhr = new XMLHttpRequest(); - - xhr.onreadystatechange = function() { - if (xhr.readyState == XMLHttpRequest.DONE) { - - var json = JSON.parse(xhr.responseText); - if(json.success === false){ - - swal({ - title: "An error ocurred", - text: json.description, - type: "error" - }, function(){ - if(reloadOnError){ - localStorage.removeItem("token"); - location.reload(); - } - }) - - return; - } - - localStorage.token = token; - upload.token = token; - return upload.prepareUpload(); - } - } - xhr.open('GET', '/api/tokens/verify', true); - xhr.setRequestHeader('type', 'client'); - xhr.setRequestHeader('token', token); - xhr.send(null); + axios.post('/api/tokens/verify', { + type: 'client', + token: token + }) + .then(function (response) { + + if(response.data.success === false){ + swal({ + title: "An error ocurred", + text: response.data.description, + type: "error" + }, function(){ + if(reloadOnError){ + localStorage.removeItem("token"); + location.reload(); + } + }) + return; + } + + localStorage.token = token; + upload.token = token; + return upload.prepareUpload(); + + }) + .catch(function (error) { + return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error"); + console.log(error); + }); + } upload.prepareUpload = function(){ -- cgit v1.2.3