Jim Jim - 5 months ago 19x
Javascript Question

XMLHttpRequest: need to use success and error functions from ajax

I have this ajax post function run when an upload button is clicked, the files get uploaded to the server, the server sees for any errors and if there are erros, it notifies the user with the

function. The problem is, since then, I've moved to
(to use the onprogress functions that it provides) but I still need to use those success and error functions from ajax. Is there a way to use them somehow with
? Thank you!

This is the code I have so far:

var xhrVideo = new XMLHttpRequest();
xhrVideo.open('POST', '/uploadVideo', true);
xhrVideo.upload.onprogress = function(e) {
if (e.lengthComputable) {
$('.videoProgress').html(((e.loaded / e.total) * 100).toFixed(0)+'%');
var videoForm = new FormData($('.videoUploadForm')[0]);

And the ajax code:

var videoData = new FormData($('.videoUploadForm')[0]);
url: '/uploadVideo',
type: 'POST',
data: videoData,
cache: false,
contentType: false,
processData: false,
success: function(data){
case '1':
alert('Wrong Video File Extension');
case '2':
alert('Wrong Image File Type');
error: function(data){
alert('Something went wrong! Please reload this page')


Use listeners for load and error events.

Example adding a listener for success event.

xhrVideo.addEventListener('load', function(e) {

NOTE: the listeners must be added before the send() function

Also, I advise you to read the entire article about using XMLHttpRequest().