I have a piece of Dart in my server-side app and I have no idea how to transform the map delivered by MySql into a JSON-type structure, to send these data to the client.
Secure: no way of Sql-injection;
easy: only a few line of code to convert data, taking in account that a string contains string-delimiters (' or "), that don't allow for a simple concat.
On the client I decode a JSON string and populate the form.
this is the server routine:
void handlePost(HttpRequest req) {
HttpResponse res = req.response;
req.listen((List<int> buffer) {
addCorsHeaders(res);
AsciiDecoder a = new AsciiDecoder();
String s = a.convert(buffer); // contains key data in JSON-string
Map data = JSON.decode(s);
if (data.containsKey("key")){
var r1 = data{"key"};
pool.query('select fi, la, age from person where keyT =' + r1).then((result) {
result.forEach((row) {
print("FirstName: ${row[0]}, lastName: ${row[1]}, Age: ${row[2]}");
closeRes(res, JSONdata);
});
});
}
else closeRes(res, "NOK missing key");
},
onError: printError);
}
Where I wrote print("FirstName: ${row[0]}, lastName: ${row[1]}, Age: ${row[2]}");
I'd write some code that transforms the MySql map into something, that I can transform in the client to a JSON-something.
On the Client I have this:
void getDataFromServer(){
HttpRequest request = new HttpRequest(); // create a new XHR
request.onReadyStateChange.listen((_) {
if (request.readyState == HttpRequest.DONE &&
(request.status == 200 || request.status == 0)) {
extractJson(request.responseText); // output the response
}
});
request.open("POST", url, async: false);
request.send(jsonData); // POST string like: {"key":"12345"}
}
Can someone some lines of code, showing how-to? or show me an article that has some working example? I'm running newest version of Dart.
I used (for 15 years) to write desktop app's in VBasic, trying now to convert.