Dart如何检查请求是否是ajax请求

时间:2013-08-06 13:02:20

标签: dart

正如标题所说,如何检查请求是否是ajax请求?这就是我试图获得它的方式,但它不起作用:

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>

点击收听元素后,它什么也没做。所以我想有两个问题:

1)如何检查请求是否是ajax请求,

2)为什么上面的代码在点击了监听器p之后没有触发回调,我希望p1文本因为这行p1.text = r.response;而被更改

1 个答案:

答案 0 :(得分:1)

您必须在XHR中指定 url 参数的方案:

HttpRequest.request('http://127.0.0.1:8080', method: 'GET')