Skip to content

Instantly share code, notes, and snippets.

@AlexDemian
Last active October 22, 2018 08:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexDemian/b493f9596be431438be2018ebb6eb4d3 to your computer and use it in GitHub Desktop.
Save AlexDemian/b493f9596be431438be2018ebb6eb4d3 to your computer and use it in GitHub Desktop.
FrontEnd snippets
<script>
function send_post() {
request = $.ajax({
url: "/url/",
type: "post",
data: 'forn_name='+JSON.stringify({'key':'value'})
});
request.done(function (response, textStatus, jqXHR){
console.log("Succesful!");
});
request.fail(function (jqXHR, textStatus, errorThrown){
console.error(
"The following error occurred: "+
textStatus, errorThrown
);
});
}
</script>
<script>
function readFile(file) {
var http = new XMLHttpRequest();
http.open('get', file);
http.onreadystatechange = function () {
text = http.responseText;
console.log(text);
};
http.send();
};
</script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Title</title>
</head>
<body>
<input type="file" name="afile" id="afile" accept="img/*"/>
<script>
document.querySelector('#afile').addEventListener('change', function(e) {
var file = this.files[0];
var fd = new FormData();
fd.append("binary_data", file);
fd.append("fname", document.querySelector('#afile').value)
var xhr = new XMLHttpRequest();
xhr.open('POST', '../path/to_cgi_script.py', true);
xhr.upload.onprogress = function(e) {
if (e.lengthComputable) {
var percentComplete = (e.loaded / e.total) * 100;
console.log(percentComplete + '% uploaded');
}
};
xhr.onload = function() {
alert(this.response);
if (this.status == 200) {
var resp = JSON.parse(this.response);
console.log('Server got:', resp);
}
};
xhr.send(fd);
}, false);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment