简体   繁体   中英

ajax request with prototype returns 200 success with blank html page (intermittent)

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

    }
 });

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.

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.

eg

<?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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM