0

When I write this Dart code :

for(int i=0;i<nbAleas;i++){
   HttpRequest request=new HttpRequest();
   // end of request event
   request.onReadyStateChange.listen((_) {
      if (request.readyState == HttpRequest.DONE &&
         (request.status == 200 || request.status == 0)) {
          handleResponse(request.responseText);
      }
   });

   // error event
   request.onError.listen((Object error)=>handleError(error));
   // method and url
   request.open("GET", urlServiceRest);
   // send the request
   request.send();
}

the request is sent only once. I verified it on the server. If I modify the opening like this :

request.open("GET", urlServiceRest, async:false);

it works. Why should the requests be synchronous ?

Also, the above requests are made to the same URL with the same parameters, for example "localhost:8080/random/10/20". If I send to async requests to this URL, only one is sent as said above. If for the second request, I change some parameters "localhost:8080/random/11/21", the two async requests are sent.

Can anyone explain this strange behavior ? Thanks in advance.

Florian Loitsch
  • 7,698
  • 25
  • 30
Serge Tahé
  • 1,869
  • 2
  • 19
  • 20
  • 1
    I do not have an answer for this, but it sounds like a caching behaviour. Anyways, I would rather "batching" your requests instead of creating a loop with many HttpRequest due to performance reasons. – Rafa Jul 05 '13 at 15:55
  • 3
    You can test if it's caching by appending something like: `'?nocache=${DateTime.now().millisecondsSinceEpoch}'` to your urls. This will guarantee that each url is unique. – beatgammit Jul 06 '13 at 06:24
  • 1
    Good guess for the two of you. It was a matter of browser caching. I changed my URL in localhost:8080/random/10/20/$i where $i is a counter for differentiating each request. Now the successive async requests work. Thanks to the two of you. – Serge Tahé Jul 08 '13 at 08:15

1 Answers1

0

The same GET requests will definitely be a candidate for caching by the browser. In addition to appending some random junk to the URL, you could try switching to POST requests; which are not cacheable unless the response includes appropriate Cache-Control or Expires header fields.

Danny Tuppeny
  • 40,147
  • 24
  • 151
  • 275