views:

66

answers:

3

hi,

I have again a problem with my ajax requests. my architecture hasn't change, so I use a Java servlet on the server side, and on the client I'm running a JavaScript application, programmed and tested with firefox. In firefox I also installed firebug, because it's very comfortable to watch the incoming and outgoing ajax requests. however, my problem is the following:

Sometimes, I have to make more ajax requests. I always check before I send a new one, if the XMLHttpRequest object is ready to send (so if it is in state 0 or 4) and only then I fire. A normal ajax request takes approximately 200 to 300 ms. But sometimes, I see responses in firebug after 10 or 30 ms, which didn't contain any data (but the request arrives the servlet). But when I dump the response, that I want to send to the client im my servlet, I see the correct string. So it seems that the servlet didn't send it to the client. I tried a flush() in the servlet but nothing changed. I have also already implemented the advice from my previous post (http://stackoverflow.com/questions/1298051/problem-with-ajax-responses), that I shouldn't make the PrintWriter object, you get with res.getWriter() (HttpServletResponse res) global (so now it is local in my doPost method).

Does anybody have an idea what's going wrong?

A: 

What is the http status (200, 304, etc) on the responses that come back very quickly?

Andy Gaskell
hi,normally, I see that in firebug, beside the time the ajax response took. but the respones, which arrive very quickly, it seems that they didn't have one. because firebug didn't show anything.
I have now checked if I get any information with the method httpReqObj.status, but again, nothing. It seems that the XMLHttpRequest object does not wait for the response? Or maybe it tooks so long and it gives up? but that would be a contradiction to the time I see in firebug
A: 

Here is the code I use in JavaScript to fire a new request:

// the XMLHttpRequest object I created in the constructor is called this.httpReqObj
// this.queryAddr is the servlet that handels the responses
if(this.httpReqObj == null || this.queryAddr == null)
{
  return(false);
}

// check if XMLHttpRequest object is ready to send a new request.   
if(this.getReadyState() != 0 && this.getReadyState() != 4)
{    
  return(false);
}

// the message queue contains strings, that should be sent to the server
if(this.msgQueue.hasNext())
{
  var bundleMsg = "";
  var bundleCtr = 0;

  while(this.msgQueue.hasNext() && bundleCtr < DataRequester.BUNDLE_FACTOR)
  {
    bundleCtr ++;
    bundleMsg += this.msgQueue.consume() + "&";
  }

  // remove last character of string
  bundleMsg = bundleMsg.substring(0, bundleMsg.length - 1);

  // "POST": choose the transport mechanism to the server
  // "this.queryAddr": URL to which the query should be sent
  // "true": communication should be asynchron 

  // if argument not available, assume method is true     
  if(typeof method == 'undefined')
  {
    method = true;
  }

  this.httpReqObj.open("POST", this.queryAddr, method);

  // this function is called every time the status
  // of the http request has changed. There exist five states.
  // 0: not initialized
  // 1: currently loading
  // 2: finished loading
  // 3: waiting for return
  // 4: finished
  if(method)
  {
    this.httpReqObj.onreadystatechange = createObjectCallback(obj, func);
  }

  // create header for POST query
  this.httpReqObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  this.httpReqObj.setRequestHeader("Content-length", bundleMsg.length);   

  // send data to server
  this.httpReqObj.send(bundleMsg);

  // synchronous request
  if(method == false)
  {
    return(this.getResponseXML());
  }

}else
{
  return;
}
A: 

Try disabling browser cache to determine if those calls are short because FF used a previously cached request.

You may also want to use something like Fiddler web debugger to ensure that the server side is returning the correct data as it shows you the http traffic before the browser has processed it

Brinley Ang