我正在做客户端服务器应用程序(通过带有postgrelsql的浏览器)。我创建了服务器,它在一个端口上运行。如何从服务器获取数据到客户端。 客户方:
req=new HttpRequest();
req.open('get', 'http://127.0.0.1:8082/');
var rr=req.send();
print(rr);
服务器端:
HttpServer server = await HttpServer.bind('127.0.0.1', 8082);
await for (HttpRequest request in server) {
request.response.headers.add("Access-Control-Allow-Origin", "*");
request.response.headers.add("Access-Control-Allow-Methods", "POST,GET,DELETE,PUT,OPTIONS");
request.response.write(await consoledatabaseconnection.someList());
handleRequest(request);
request.response.close();
}
void handleRequest(HttpRequest request) {
try {
if (request.method == 'GET') {
print('request get');
request.response.write('data form server');
} else {
print('request others');
}
} catch (e) {
print('Exception in handleRequest: $e');
}
print('Request handled.');
}
在服务器端,如何返回数据。
答案 0 :(得分:1)
服务器端代码
$(function() {
$( "#datepicker" ).datepicker({
dateFormat: 'dd/mm/yy',
changeMonth: true,
changeYear: true
});
});
<input type="text" id="datepicker"></p>
客户端代码 req = new HttpRequest();
main(List<String> arguments) async {
HttpServer.bind('localhost', 8085).then((server) {
server.listen((HttpRequest request) {
request.response.headers.add("Access-Control-Allow-Origin", "*");
request.response.headers
.add("Access-Control-Allow-Methods", "POST,GET,DELETE,PUT,OPTIONS");
request.response.headers.add('Access-Control-Allow-Headers',
'Origin, X-Requested-With, Content-Type, Accept,application/x-www-form-urlencoded');
if (request.method == "POST") {
List<int> dataBody = new List<int>();
request.listen(dataBody.addAll, onDone: () async {
query_dy = new String.fromCharCodes(dataBody);
request.response.write(await databaseconnection('your response code here'));
request.response.close();
});
}
});
});
}