Tinyweb and XMLHttpRequest
Posted: Thu Apr 16, 2020 6:02 pm
I'm using Tinyweb library on ESP32. I need to send JSON from website to ESP32 and from ESP32 to website.
So that's my code from website:
For json request:
For json send:
And that's my code on ESP32 (routes part):
When I try to get JSON from ESP using website I get error 405.
But when I simply write an address 192.168.4.1:8081/DATAJson i get the text of json in web browser. So I guess that code on ESP is correct.
When I try to send JSON to ESP I don't see any reaction.
What I'm doing wrong?
So that's my code from website:
For json request:
Code: Select all
var xhttpData = new XMLHttpRequest();
xhttpData.open('POST', 'DATAJson', true);
xhttpData.setRequestHeader("Content-Type", "application/json");
xhttpData.onreadystatechange = function()
{
if (xhttpData.readyState === 4)
{
var DATAJson = xhttpData.response;
console.log(DATAJson)
}
}
xhttpData.send();
Code: Select all
var xhttp2 = new XMLHttpRequest();
xhttp2.open('POST', 'DATAJsonRecv', true);
xhttp2.setRequestHeader("Content-Type", "application/json");
xhttp2.onreadystatechange = function()
{
if (xhttp2.readyState === 4)
{
alert("Data sent");
}
}
xhttp2.send('{"JsonTestValue":5}');
Code: Select all
@app.route('/DATAJson')
async def index(request, response):
await response.send_file('data.json')
@app.route('/DATAJsonRecv')
async def index(request, response):
await print(request)
But when I simply write an address 192.168.4.1:8081/DATAJson i get the text of json in web browser. So I guess that code on ESP is correct.
When I try to send JSON to ESP I don't see any reaction.
What I'm doing wrong?