Skip to content

Instantly share code, notes, and snippets.

@kn9ts
Last active January 14, 2016 14:03
Show Gist options
  • Save kn9ts/6def62efa397459b81c7 to your computer and use it in GitHub Desktop.
Save kn9ts/6def62efa397459b81c7 to your computer and use it in GitHub Desktop.
Custom Ajax Request
$.ajax({
url: URI,
type: "POST",
xhr: function() { // Custom XMLHttpRequest
var xhr = $.ajaxSettings.xhr();
if (xhr.upload) { // Check if upload property exists
xhr.upload.addEventListener('progress', function(e) {
if (e.lengthComputable) {
//Show the uploading progress
setTimeout(function() {
if ((e.loaded / e.total) === 1) {
$('.progress-bar-pink').animate({
'width': '100%'
}, 500, function() {
//remove modal;
$('#progress-header').text("Your story was safely stored.");
setTimeout(function() {
//Remove the modal
$('.modal-scrollable').click();
$('.progress-bar-pink').css({
'width': '0%'
});
}, 2000);
});
} else {
$('.progress-bar-pink').css({
'width': ((e.loaded / e.total) * 100) + "%"
});
}
// console.log("uploaded -- " + ((e.loaded / e.total) * 100) + "%");
}, 10);
console.log("Total data size -- " + (e.total / 1024) + " Kbs");
}
//Erase all form data for refresh uploading
formdata = undefined;
}, false); // For handling the progress of the upload
}
return xhr;
},
data: formdata,
cache: false,
contentType: false,
processData: false,
// dataType: "text",
beforeSend: function(xhr) {
/*
* Prevent this error below because of CORS:
* @error -- XMLHttpRequest cannot load http://djotjog.com/c/saveaudio.
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:2323' is therefore not allowed access.
*/
// xhr.setRequestHeader("Access-Control-Allow-Origin", "true");
$("body").modalmanager("loading");
//pause the player if playing
document.getElementById("recorded-audio").pause();
}
}).done(function(response) {
//do something
if (response && response.result) {
$('.modal-scrollable').click(); //close overlay
alert(response.message);
}
}).fail(function(error) {
alert("An error occured -- " + JSON.stringify(error));
$('.modal-scrollable').click();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment