tags:

views:

192

answers:

2

I have a problem where sometimes the data payload in the AJAX code below is not sent to the server.

// loans_url, sample_id, sample defined above this block
$.ajax({
  type:       "POST",
  url:        loans_url,
  data:       {'loan[sample_id]':sample_id},
  beforeSend: function()     { sample.remove(); },
  success:    function(data) { $('#basket table tr:last').after(data); },
  error:      function()     { $('#results').prepend("Apologetic error message..."); }
});

A "good" post sends data looking like this: loan[sample_id]: 1234. A "bad" post hits the correct URL but does not send any data at all.

I cannot reproduce this in development yet it happens about once a day in production. In Firebug I tried sabotaging the markup in the page from which sample_id is obtained, but that simply caused loan[sample_id]: undefined to be sent, which is not the problem I am facing.

Is there any way the data payload could be not sent at all? Can you suggest how I might debug this further? (This is all with jQuery 1.3.2.)

A: 

The post data would only be lost if there was a connectivity issue - for example, you could replicate this by pulling your network cable out just as your press "Submit" - the headers MIGHT make it, the post data MIGHT not - the headers are always sent first.

So, I think you're looking at infrastructure. A lost internet connection, some missing packets or something similar. I don't think that's a problem for a developer to solve - just as long as you only accept fully formed post requests.

Sohnee
That's certainly possible. However there are many other AJAX calls on the site and this is the only one that ever has a problem. The probability of only this particular call being affected seems pretty small...so I suspect some other problem.
Andy Stewart
+1  A: 

Try to define a error handler for your jquery ajax call and log the error data somewhere (uh, probably make another ajax call to a error logger?) If it's a connectivity issue it will be much more difficult to log anything remotely. Depending on your app, you may want to log something locally (cookies maybe) for posterior analysis (once the connection is up, assuming its a connectivity issue).

Miguel Ping
I'd try this. Use the error event to log any anomaly. Or, just use it as breakoint.
Mike
Do you mean a pre-submit error handler to catch any problems the Javascript has putting together the AJAX post? Not sure how I would implement that.I have an error handler on the server (which is how I know when this happens). The post request is always well formed and successfully submitted; it just sometimes lacks the `loan[sample_id]: 1234` payload.
Andy Stewart
jQuery allows both `error` callbacks for erroneous ajax calls and `beforeSend` callbacks to modifiy the XMLHttpRequest. Check out thejQuery [AJAX][1] docs. [1]: http://docs.jquery.com/Ajax/jQuery.ajax
Miguel Ping
By the way, is your error handler the first handler on the whole stack? It may be that other error handlers are causing the error...
Miguel Ping
I've just edited my question to show where my error handler is. However the missing-payload problem was happening before I had any error handlers at all.
Andy Stewart