我正在为 Http GET 请求创建一个包装类,但是当我使用 时HttpRequest.request
,我没有得到任何响应。如果我使用原始 HttpRequests 使用等效代码,它可以工作。
我错过了什么吗?我正在使用 Dart SDK 0.8.10.3_r29803。
class AsyncGet<Output> {
Future<Output> request(String url, Output process(String response)) {
// The following works
var request = new HttpRequest();
request.open("GET", url);
request.send(null);
request.onReadyStateChange.listen((_) {
if (request.readyState == HttpRequest.DONE &&
(request.status == 200)) {
Output out = process(request.responseText);
_completer.complete(out);
}
});
// The following doesn't work ("Blah" doesn't get logged)
// HttpRequest.request(url, method: 'GET').then((r) {
// Logger.root.info("Blah");
// if (r.readyState == HttpRequest.DONE &&
// (r.status == 200)) {
// Output out = process(request.responseText);
// _completer.complete(out);
// }
// })
return _completer.future;
}
Completer<Output> _completer = new Completer<Output>();
}