Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use AJAX in Dart with async = true

Tags:

ajax

dart

I found a reference for an XMLHttpRequest

  final req = new XMLHttpRequest();
  req.open('GET', '${Sections.home}/data/$name', false);
  req.send();
  _htmlBody = req.responseText; 

Full Source

But this is for async = false, is there a working example of async = true?

like image 648
Utilitron Avatar asked Nov 14 '11 15:11

Utilitron


1 Answers

There is an example at https://code.google.com/p/dart/source/browse/branches/bleeding_edge/dart/samples/belay/bcap/bcap_client.dart

  void privateInvoke(String ser, String method, String data,
                     SuccessI ski, FailureI fki) {
    if (urlRegex.hasMatch(ser)) {
      var req = new XMLHttpRequest();
      req.open(method, ser, true);
      req.on.readyStateChange.add(void _(evt) {
        if (req.readyState == 4) {
          if (req.status == 200) {
            ski(req.responseText);
          } else {
            fki(new BcapError(req.status, req.statusText));
          }
        }
      });
      req.send(data);
      return;
    } else {
      super.privateInvoke(ser, method, data, ski, fki);
    }
  }

also another asynchronous example in https://code.google.com/p/dart/source/browse/branches/bleeding_edge/dart/client/samples/total/src/ServerChart.dart

XMLHttpRequest request = new XMLHttpRequest();
request.on.readyStateChange.add((Event event) {
  if (request.readyState == XMLHttpRequest.DONE && request.status == 200) {
    callback("data:image/png;base64,${StringUtils.base64Encode(request.responseText)}");
  }
});
...
request.open("POST", url, true, null, null);
request.setRequestHeader("Content-type", "text/plain");
request.overrideMimeType("text/plain; charset=x-user-defined");
print("Chart request: ${data}");
request.send(data);
like image 95
Duncan Avatar answered Oct 29 '22 00:10

Duncan