var wifi = require("Wifi");
var page = "<!DOCTYPE html>\r\n<html>\r\n<body>\r\n\r\n";
page += "<form action=\"\" method=\"post\">\r\n First name: ";
page += "<input type=\"text\" value=\"\" name=\"fname\"><br>\r\n ";
page += "Last name: <input type=\"text\" value=\"\" name=\"lname\"><br>\r\n ";
page += "<input type=\"submit\" value=\"Submit\">\r\n</form>\r\n\r\n";
page += "<p>Click on the submit button, and the input will be sent to Espruino.";
page += "</p>\r\n\r\n</body>\r\n</html>";
var b;
function onPageRequest(req, res) {
if (req.method == "GET") {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(page);
} else {
req.on("close", function () {
console.log("AVAIL:" + req.available());
b = req.read();
console.log("BODY: " + b);
});
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end("ok");
}
}
require("http").createServer(onPageRequest).listen(8080);
var b consist all your data, but data requires parse. (example data: "fname=Pavel&lname=123&xname=rtavel&zname=12673")
for do this i developed little parser:
var str = "fname=Pavel&lname=123&xname=rtavel&zname=12673";
function splitData(data) {
var dataReady = [];
var part = true;
var commVal = "";
for (var index = 0; index < data.length; index++) {
if (part) {
if (data[index] != '=') commVal += data[index];
else {
dataReady.push(commVal);
commVal = "";
part = false;
}
}
else {
if (data[index] != '&') commVal += data[index];
if (data[index] == '&') {
dataReady.push(commVal);
commVal = "";
part = true;
}
if (index == data.length - 1) {
dataReady.push(commVal);
return dataReady;
}
}
}
}
console.log(splitData(str));
or more simple variant parser:
var str = "fname=Pavel&lname=Dubailo&xname=TEST"
var arr = str.split("&")
var result = {};
arr.forEach(function (pair) {
var elem = pair.split("=");
result[elem[0]] = elem[1];
});
console.log(result);
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
...and also more correct variant
var b consist all your data, but data requires parse. (example data: "fname=Pavel&lname=123&xname=rtavel&zname=12673")
for do this i developed little parser:
or more simple variant parser: