Forked from ivansifrim/gist:22faebb250bc009beb0a
Last active
August 29, 2015 14:12
-
-
Save rkstedman/81abae9149a72b0ea2f8 to your computer and use it in GitHub Desktop.
Accessing AJAX response
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(function() { | |
function getFormData(cb){ | |
jQuery.ajax({ | |
type:'GET', | |
url: 'http://localhost:3000/s3signature.json', | |
error: function(jqXHR, textStatus, err){ | |
console.log('error!', textStatus); | |
cb(err); | |
}, | |
success: function(data){ | |
console.log('success!'); | |
cb(null, data); | |
} | |
}) | |
}; | |
// you cannot have the data here because getFormData() has not been called yet | |
console.log("I want the data printed here: "); | |
// here is where getFormData is called | |
// you can print data by passing in a callback function | |
console.log(getFormData(function(err, data) { | |
console.log('was there an err?', err); | |
console.log('here is the data', data); | |
})); | |
// I'm not quite sure what this has to do with the GET request? | |
// this appears to simply upload a file to s3 | |
$('#select-file').fileupload({ | |
maxFileSize: 100000000, | |
fileInput: $(this), | |
url: 'https://ivancoffeetime.s3.amazonaws.com/', | |
type: 'POST', | |
autoUpload: true, | |
formData: getFormData(), | |
paramName: 'file', // S3 does not like nested name fields i.e. name="user[avatar_url]" | |
dataType: 'XML', // S3 returns XML if success_action_status is set to 201 | |
replaceFileInput: false, | |
progressall: function (e, data) { | |
var progress = parseInt(data.loaded / data.total * 100, 10); | |
$("#progress-bar").css('width', progress + '%') | |
console.log($("#progress-bar").css('width')); | |
}, | |
start: function (e) { | |
$("#progress-bar").css('background', 'green'); | |
$("#progress-bar").css('display', 'block'); | |
$("#progress-bar").css('width', '0%'); | |
$("#progress-bar").text('Loading file...'); | |
}, | |
done: function(e, data) { | |
$("#progress-bar").text("Uploading done"); | |
// extract key and generate URL from response | |
var key = $(data.jqXHR.responseXML).find("Key").text(); | |
var url = 'https://ivancoffeetime.s3.amazonaws.com/' + key; | |
console.log("THIS IS THE URL: " + url); | |
$("#thumbnail").html('<img src="'+url+'" style="width:100px;">'); | |
}, | |
fail: function(e, data) { | |
// submitButton.prop('disabled', false); | |
console.log(data); | |
$("#progress-bar"). | |
css("background", "red"). | |
text("Failed"); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment