Last active
October 22, 2018 08:15
-
-
Save AlexDemian/b493f9596be431438be2018ebb6eb4d3 to your computer and use it in GitHub Desktop.
FrontEnd snippets
This file contains hidden or 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
<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> |
This file contains hidden or 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
<script> | |
function readFile(file) { | |
var http = new XMLHttpRequest(); | |
http.open('get', file); | |
http.onreadystatechange = function () { | |
text = http.responseText; | |
console.log(text); | |
}; | |
http.send(); | |
}; | |
</script> |
This file contains hidden or 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
<!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