Skip to content

Instantly share code, notes, and snippets.

@AlexDemian
Last active October 22, 2018 08:15
Show Gist options
  • Select an option

  • Save AlexDemian/b493f9596be431438be2018ebb6eb4d3 to your computer and use it in GitHub Desktop.

Select an option

Save AlexDemian/b493f9596be431438be2018ebb6eb4d3 to your computer and use it in GitHub Desktop.

Revisions

  1. AlexDemian revised this gist Oct 22, 2018. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions js_read_file
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    <script>
    function readFile(file) {
    var http = new XMLHttpRequest();
    http.open('get', file);
    http.onreadystatechange = function () {
    text = http.responseText;
    console.log(text);
    };
    http.send();
    };
    </script>
  2. AlexDemian revised this gist Oct 17, 2018. 1 changed file with 20 additions and 0 deletions.
    20 changes: 20 additions & 0 deletions ajax_post
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    <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>
  3. AlexDemian created this gist Jul 13, 2018.
    37 changes: 37 additions & 0 deletions upload_file_jquery.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    <!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>