Feb-20-2020, 06:14 PM
I have a working jquery file that sends data in json format to a wsgi file as follows:
How do I get the json data?
Below is the wsgi code:
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 |
$(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() { }, }); }); }); |
Below is the wsgi code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
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() |