Ajax发布被“取消" [英] Ajax post getting "cancelled"

查看:89
本文介绍了Ajax发布被“取消"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ajax帖子,但是它被取消了.我刚刚制作了一个test.php文件,应该将其设置为echo 'test';值.

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.

这是我的ajax脚本的样子:

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);
        }
    });
}

该函数被调用,我尝试通过控制台记录它.变量也可以通过.这就是我在chrome网络标签中看到的内容. 屏幕截图

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

除此之外,我很无奈.

我这样调用函数:

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

$adl .= '<script type="text/javascript">
              loadAd(\''.$datum.'\');
         </script>';

推荐答案

我们是从链接发出ajax请求,而不是阻止链接被遵循.因此,如果您在onclick属性中执行此操作,请确保返回false;否则,请返回false.以及

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;
 }

这篇关于Ajax发布被“取消"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆