x

Webserver TXT (firmware >4.5.1) examples

Gepost 2019/08/25
 Simple example, shows the basics for the use of AJAX.

About the .ajax jScript function, see:   https://api.jquery.com/jQuery.ajax/

 

The HTML file:

(You need to change the IP address into the IP-address of your TXT)

 
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8">
        <title>test</title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
        <script>
            $(function()
            {
                $('#clickme').click(function(){
                    alert('Im going to start processing');
                    var jqxhr =$.ajax({
                        url: "http://192.168.10.170/ajaxpost.cgi",
                        type: "post",
                        datatype:"text",
                        data: {'key':'value'},
                        success: function(response){
                          alert('Succes');
                            alert('response '+response);
                         },
                         error: function(response){
                            alert('AJAX failure');
                        },
                       })
                      .done(function() {
                         alert( ".done success" );
                       })
                     .fail(function() {
                         alert( ".fail error" );
                        })
                       .always(function() {
                         alert( ".always complete" );
                     
                  });
               });
            });
        </script>
    </head>
    <body>
        <button id="clickme"> click me </button>
    </body>
</html>
 

The CGI file ajaxpost.cgi

 
#!/usr/bin/python3
import sys
import json
import cgi
 
sys.stdout.write("Content-Type: application/plain")
sys.stdout.write("\n")
sys.stdout.write("\n")
sys.stdout.write("Content-Type: application/plain")
sys.stdout.write("\n")
 
sys.stdout.close()