0

As the title says, how to check if the request is an ajax request? This is how I tried to get it, but it is not working :

test_io.dart :

import 'dart:io';

void main() {
  HttpServer.bind('127.0.0.1', 8080)
    .then((HttpServer server) {
      server.listen((HttpRequest request) {
        HttpResponse response = request.response;
        response.write(request.header);
        response.close();
      });
    });
}

test_html.dart :

import 'dart:html';

void main() {
  ParagraphElement p = query('p#text');
  ParagraphElement p1 = query('p#text1');
  p.onClick.listen((MouseEvent event) {
    HttpRequest.request('127.0.0.1:8080', method: 'GET')
      .then((HttpRequest r) {
        p1.text = r.response;
      });
  });
}

test.html :

<html>
  <head>
    <title>testing</title>
  </head>

  <body>
    <p id="text">COBA</p>
    <p id="text1"></p>
    <script type="application/dart" src="testing.dart"></script>
    <script src="packages/browser/dart.js"></script>
  </body>
</html>

After clicking the listening element, it did nothing. So I guess there're 2 questions :

1)How to check if the request is an ajax request, and

2)Why the above code, after clicking the listener p, didn't trigger the callback, I expect that the p1 text get changed because of this line p1.text = r.response;

Budi Sutrisno
  • 599
  • 1
  • 5
  • 12
  • Are you loading your _test.html_ with _http://127.0.0.1:8080/***/test.html_ ? If not your XHR violates the cross origin policy. See http://stackoverflow.com/a/13589527/634863. – Alexandre Ardhuin Aug 06 '13 at 13:12
  • Yes, but I disabled it with `--disable-web-security` flags, so I think that is not the issue. – Budi Sutrisno Aug 06 '13 at 13:16

1 Answers1

1

You have to specify the scheme for the url parameter in your XHR :

HttpRequest.request('http://127.0.0.1:8080', method: 'GET')
Alexandre Ardhuin
  • 71,959
  • 15
  • 151
  • 132