jczic / MicroWebSrv

A micro HTTP Web server that supports WebSockets, html/python language templating and routing handlers, for MicroPython (used on Pycom modules & ESP32)
https://github.com/jczic/MicroWebSrv
MIT License
640 stars 115 forks source link

how to send updated json data from microwebserver to json file that embedded in html file #43

Closed m2030 closed 5 years ago

m2030 commented 5 years ago

great project it is easy and simple how to send updated json data from microwebserver to json file that embedded in html file with jquery ajax

`<!DOCTYPE HTML>

` like with micropython code ` import gc import os gc.collect() from microWebSrv import MicroWebSrv array=[80,90,86,89,90,89,86,95,94,91,89,97,94,100,110] array1='[' def _httpHandlerarray(httpClient, httpResponse): gc.collect() for x in array: array1=str(x+7) array1=',' array1[-1]=']' ## f=open('/www/livegraph2.json','w') ## f.write(mystring) ##f.close() ##print(mystring) httpResponse.WriteResponseOk(headers=None, contentType= "application/json", contentCharset="UTF-8", content=array1) routeHandlers=[("/livegraph2.html","GET",_httpHandlerarray)] srv=MicroWebSrv(routeHandlers=routeHandlers,webPath='/www/') gc.collect() print(array1) srv.Start(threaded=False)`
jczic commented 5 years ago

Hello :) Sorry for latence !

You can just respond in JSON format from a handler function like that (check main.py) :

    httpResponse.WriteResponseJSONOk({
        'example1'    : 12345,
        'example2'    : True,
        'example3'    : 'toto'
    })
m2030 commented 5 years ago

thanks jczic