aboutsummaryrefslogtreecommitdiffstats
path: root/test/sample-post.html
blob: d28c5bc0aa0a3532b523523c241ef9211f43e5b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<html>
  <head>
    <title>Sample Post test</title>
  <body>
    <h1>Sample Post test</h1>
    
    <h2>Sample Post File</h2>
    <form enctype="multipart/form-data">
        <input type="file" name="file"/>
        <input type="hidden" name="hidden" value="bollobollo" />
        <br>
        <button formmethod="POST" formaction="api/post/upload-image">Post File</button>
    </form>
    
    <h2>Sample Post JSON</h2>
    
    <form id="jsonform">
        <input name='name' value='MyName'>
        <input name='info' value='MyInfo'>
        <select name='option'>
        <option selected>opt1</option>
        <option>opt2</option>
        <option>opt3</option>
        </select>
        <label>ticked</label>
        <input type='checkbox'  name='ticked'>
    </form>
    <p><input id="jsonrep" placeholder="AFB-daemon Response" readonly style="width: 100%">
    <button onclick="xpost();">Post JSON</button>

    <script>
    // post bouton press post form as JSON
    var xpost=function() {
        var jform={};
        var xform = document.querySelector('#jsonform').elements;
        var xreqt = new XMLHttpRequest();
        xreqt.open("POST", "api/post/upload-json", true);
        xreqt.setRequestHeader('Content-Type', 'application/json');
        
        // Serialise jform in JSON
        for(var ind = 0; ind < xform.length; ind++) {
            console.log ("name=%s value=%s", xform[ind].name, xform[ind].value);
            jform[xform[ind].name] = xform[ind].value;
        }

        // display afb-daemon return values
        xreqt.onload = function () {
            var result = "Status:" + xreqt.status + " Value:" + xreqt.responseText;
            document.getElementById("jsonrep").value = result;
            console.log (result);
        };
        
        // Post Form as JSON
        console.log ("Posting jform=%j", jform); 
        xreqt.send(JSON.stringify(jform)); 
    };
    </script>

    
    </body>
</html>