Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ajax post getting "cancelled"

Tags:

jquery

ajax

I have an ajax post, but it's getting cancelled. I just made a test.php file which is just supposed to echo 'test'; value.

This is how my ajax script looks like:

function loadAd(date) {
    var ajax_url = '<?=URL?>components/ajax/grab_ads.php';
    $.ajax({
        type: 'POST',
        url: ajax_url,
        dataType: 'html',
        data: ({
            date : date
        }),
        cache: false,
        success: function(data) {
            if(data > 0) {
                $('.advert').fadeIn("fast");
            }       
        },
        error: function(xhr, textStatus, errorThrown) {
            //alert('Nastala chyba. ' + errorThrown);
        }
    });
}

The function is called, I tried console logging it. The variable is passed okay as well. This is what I'm getting in chrome network tab. screenshot

Other than that I am quite helpless.

Edit: I call the function like this:

$datum = Date("d-m-Y H:i:s");

$adl .= '<script type="text/javascript">
              loadAd(\''.$datum.'\');
         </script>';
like image 561
InsaneSVK Avatar asked May 16 '13 10:05

InsaneSVK


1 Answers

we were making the ajax request from a link, and not preventing the link from being followed. So if you are doing this in an onclick attribute, make sure to return false; as well

 function loadAd(date) {
  var ajax_url = '<?=URL?>components/ajax/grab_ads.php';
  $.ajax({
    type: 'POST',
    url: ajax_url,
    dataType: 'html',
    data: ({
        date : date
    }),
    cache: false,
    success: function(data) {
        if(data > 0) {
            $('.advert').fadeIn("fast");
        }       
    },
    error: function(xhr, textStatus, errorThrown) {
        //alert('Nastala chyba. ' + errorThrown);
    }
 });
 return false;
 }
like image 129
Shijin TR Avatar answered Nov 13 '22 14:11

Shijin TR