views:

631

answers:

2

i have a page performing the following ajax request when a button is pressed.

normally i get a json object back and it works fine, i have noticed on intermittent requests (usually only the first request from that page), i get back a 200 success code with a blank page.

if i reload the html page, then press the button again it works fine straight afterwards.

by intermittent i mean i can't replicate the issue at will, but it is happening regularly enough that i need to do something about it

i am just wondering if it is most likely an ajax or in particular a prototype problem or a server side issue (i am using debian/apahce/php)

what can i try to track down the problem ?

new Ajax.Request( url, 
{
 method:'post',
 parameters: $('TeamForm').serialize(true),
 onSuccess: function(transport) {
  // do stuff      

 },
 onFailure: function(transport) { 
  // display error

 }
 });
A: 

Based on the example you provided, the only source of concern I can see with the javascript is the $('TeamForm').serialize(true); statement. Are you sure that the TeamForm has well formed data, and your PHP backend is handling this appropriately?

Check your PHP code for any trailing whitespace. You should not end your PHP files with a closing tag.

e.g.

<?php
class Foo {
}
?> //There is a space after this closing tag!

Would result in the hidden space being sent to your browser. It is valid syntax (and recommended) to leave the closing tag off of a pure PHP file:

<?php
class Foo {
}

Also check your code for any echo's or print's that may send output to the browser. Also check your display_errors setting, as an error message could result in data being sent to the browser as well.

I'd put 99:1 odds on the problem being server-side.

hobodave
according to firebug the serialize part is working fine, and i don't have any trailing spaces. thinking about it more, i do agree on it being a server side issue. one thing i have just found out is that the json_encode only likes utf8 chars, and i am not enforcing that, so i am going to do some tests and see if that is the issue
bumperbox
I wasn't implying the serialize was failing. I was suggesting that the contents of that serialization could possibly be unexpected on the server-side. Potentially causing a crash or error server-side. Do you have a demo page you can provide access to that demonstrates this problem?
hobodave
When debugging your server side script make sure to use try/catch blocks and possible a custom error handler. SInce you're expecting JSON data, any errors really need to be caught or else you'll end up with the behavior you're experiencing.
Josh
@hobodave, you are probably right about the upexpected data, eg being non utf-8 data. the current website is not public facing, but if i don't have any luck with the suggestions i will create a small public demo
bumperbox
+1  A: 

This isn't a solution to your problem but a workaround -- in the meantime, you could detect if the response's responseJSON property is NULL, and if so, log the error and resubmit the request. That way at least the second request should go through. The easiest way to handle this might be to throw a custom object from your onSuccess handler allowing your onFailure handler to catch it and resubmit.

Josh
good idea, i would prefer to fix it properly, but at least i have a fallback option now
bumperbox