Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Capture json data
#1
I'm sending form data using JavaScript through Ajax in Json format to the python script ( wsgiref ), so it can capture this data and return the response in json format. The problem I face boils down to a server error 500, where I can't reach the objective of capturing the data and being able to return the answer, below is the code:

Html, Javascript, Ajax, Json Code :
<!DOCTYPE html>
<html>
    <head>

        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
        <meta name="description" content=""/>
        <meta name="keywords" content=""/>

        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
        <style>
            .input-field input[type=email] + label { color: #455a64 !important; margin-left: 20px; margin-top: 15px; }
            .input-field input[type=email] { width: 362px; height: 70px; padding-left: 20px; padding-right: 20px;
            border: 1px solid #455a64 !important; box-shadow: 0 0 0 0 #455a64 !important; border-radius: 5px; }
            .input-field input[type=email]:focus + label { color: #0d47a1 !important; margin-left: 20px; margin-top: 20px; }
            .input-field input[type=email]:focus { border: 1px solid #0d47a1 !important; border-radius: 5px; font-size: 20px; }
        </style>
        <title>Webstrucs Form</title>
    </head>

    <body>
        <main>
            <div class="input-field">
                <input id="email" type="email" class="ws_input_email">
                <br>
                <a class="waves-effect waves-light blue darken-4 btn-large"></a>
                <br>
                <span class="preload-legend"></span>
            </div>
        </main>

        <script>
            const next = document.querySelector(".btn-large")
            const text = next.innerHTML = "Seguinte"

            next.onclick = function() {

                let ws_safe_email = document.querySelector(".ws_input_email").value

                const jsonData = {
                    "ws_safe_email": ws_safe_email
                }

                const jsonSend = JSON.stringify(jsonData)

                const ajax = new XMLHttpRequest()
                ajax.open("POST", "http://127.0.0.1:8080/", true)
                ajax.setRequestHeader("Content-Type", "application/json")
                ajax.send(jsonSend)

                if(ajax.readyState === 4) {
                    if(ajax.status === 200) {
                        alert("Received Data")
                    }
                }
            }
        </script>
    </body>
</html>
Python Code :

from wsgiref.simple_server import make_server
# Module name : cgi -> ( https://docs.python.org/3/library/cgi.html#module-cgi )
import cgi, json


def apps(environ, start_response):

    # Function -> Capture the request method:
    v_mtd = environ["REQUEST_METHOD"]
    # Function -> Capture the request form data:
    v_inp = cgi.FieldStorage(environ["wsgi.input"], environ=environ)

    if v_mtd == "GET":

        # Acessa o arquivo static requisitado :
        file = open("index.html", "r")
        # Faz a leitura do arquivo :
        sile = file.read()

        status = "200 OK"
        headers = [("Content-type", "text/html; charset=utf-8")]
        start_response(status, headers)

        # The returned object is going to be printed
        return [str(sile).encode('utf-8')]

    if v_mtd == "POST":
        ...

with make_server("", 8080, apps) as httpd:

    print("Serving on port 8080...")
    # Serve until process is killed
    httpd.serve_forever()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  encrypt data in json file help jacksfrustration 1 63 Yesterday, 05:16 PM
Last Post: deanhystad
  Read nested data from JSON - Getting an error marlonbown 5 1,309 Nov-23-2022, 03:51 PM
Last Post: snippsat
  Reading Data from JSON tpolim008 2 1,031 Sep-27-2022, 06:34 PM
Last Post: Larz60+
  Convert nested sample json api data into csv in python shantanu97 3 2,726 May-21-2022, 01:30 PM
Last Post: deanhystad
  Struggling with Juggling JSON Data SamWatt 7 1,820 May-09-2022, 02:49 AM
Last Post: snippsat
  json api data parsing elvis 0 902 Apr-21-2022, 11:59 PM
Last Post: elvis
  Serializing Python data Correctly (JSON) JgKSuperstar 4 2,039 Nov-04-2021, 07:31 PM
Last Post: JgKSuperstar
  How to save json data in a dataframe shantanu97 1 2,122 Apr-15-2021, 02:44 PM
Last Post: klllmmm
  Sort data from JSON file Dummy_in_programming 2 2,400 Jan-04-2021, 06:17 PM
Last Post: deanhystad
  Get Python json data i n SQL Server Djin 1 1,980 May-13-2020, 05:56 AM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020