1

我正在为 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>();
}
4

1 回答 1

1

好像我忘记了最后的分号......以下现在正在工作。如果我收到某种语法错误的错误消息,那就太好了。

HttpRequest.request(url).then((r) {
    if (r.readyState == HttpRequest.DONE &&
        (r.status == 200)) {
        Output out = process(r.responseText);
        _completer.complete(out);
    }
});
于 2013-11-09T01:39:20.653 回答