HttpRequest.request无法正常工作

时间:2013-11-07 09:41:28

标签: dart dart-html

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

1 个答案:

答案 0 :(得分:1)

似乎我忘记了结尾处的分号......以下是行不通的。 如果我收到某种语法错误的错误消息,那就太好了。

HttpRequest.request(url).then((r) {
    if (r.readyState == HttpRequest.DONE &&
        (r.status == 200)) {
        Output out = process(r.responseText);
        _completer.complete(out);
    }
});