Facebook Ajax call hitting your URL 3 times in a row

Did you notice Ajax calls hitting your url three times in a row?

This will happen if the URL ajax.post() is hitting doesn’t return anything at all.

var ajax = new Ajax();
ajax.responseType = Ajax.RAW;
ajax.requireLogin = false;
ajax.ondone = null;
ajax.onerror = function() {}

var params={
    'title':'Nice title',
    'comment': 'Nice comment'
};
ajax.post('http://example.com/my_ajax_handler.php',params);

If your my_ajax_handler.php script doesn’t return a single string, ajax.post will hit you 3 times.

Just return something like ‘1′ for instance at the end of your my_ajax_handler.php script and you will be hit only once:

<?php
 if(count($_POST)) {
     $title = $_POST['title'];
     // process your data...
 }
?>
1

Notice the ‘1′ outside of the PHP block at the end.

Tags: , ,

One Response to “Facebook Ajax call hitting your URL 3 times in a row”

  1. dos says:

    Excellent, that seems the same thing that was happening in my app