Python Forum
Get json data - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Get json data (/thread-24582.html)



Get json data - JohnnyCoffee - Feb-20-2020

I have a working jquery file that sends data in json format to a wsgi file as follows:

$(document).ready(function() {

    $('.btn').text('search').click(function() {
        // atribui valor a variavel
        var wsi_search = $('.ws_search').val();
        // set method ajax:
        $.ajax({
            type: 'POST',
            url: '/kosmos',
            data: { wsi_search: wsi_search },
            dataType: 'json',
            // function shoot efect:
            beforeSend : function() {
            },
            // end callback:
            complete : function() {
            },
            // shoot event moment feedback server:
            success : function() {
                alert('ok')
            },
            // monstra erro na requisição
            error : function() {
            },
        });
    });
});
How do I get the json data?
Below is the wsgi code:

from wsgiref.simple_server import make_server
from wsgiref.util import request_uri

def apps(environ, ws_output):

    ws_mtd = environ['REQUEST_METHOD']
    ws_url = request_uri(environ, include_query=True)

    if ws_mtd == 'POST':
        
        # Tratar o method post :
        status = '200 OK'
        headers = [('Content-type', 'text/html; charset=utf-8')]
        ws_output(status, headers)
        return [str('Dados Json').encode()]

with make_server('', 8000, apps) as httpd:
    print('Running wsgi\nBrowser access - http://127.0.0.1:8000\nCrl+c for exit command or Crl+z for stop')
    httpd.serve_forever()