jQuery Ajax 帖子被“取消”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16585157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:24:46  来源:igfitidea点击:

Ajax post getting "cancelled"

jqueryajax

提问by InsaneSVK

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

我有一个 ajax 帖子,但它被取消了。我刚刚制作了一个test.php应该echo 'test';有价值的文件。

This is how my ajax script looks like:

这是我的 ajax 脚本的样子:

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

该函数被调用,我尝试控制台记录它。变量也可以正常传递。这就是我在 chrome 网络选项卡中得到的。截屏

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

回答by Shijin TR

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

我们从链接发出ajax请求,而不是阻止链接被跟踪。因此,如果您在 onclick 属性中执行此操作,请确保返回 false;还有

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

回答by Gaurav sharan

Check for the HTML elementby whose event the AJAXcall is getting triggered.

检查触发AJAX调用HTML element的事件。

If it's an input button, ensure it should be of type button' <input type="button" />and use returnstatement.

如果它是一个输入按钮,确保它应该是 type button' <input type="button" />和 usereturn语句。

If it's an anchortag, try using return statement: return either trueor false.

如果它是一个anchor标签,请尝试使用 return 语句: return trueor 或false

Ensure page doesn't get refreshed on completion of the AJAXcall. If you use input field of type submit, it will submit the form and lead to page refreshed, and the request gets canceled.

确保AJAX调用完成后页面不会刷新。如果您使用提交类型的输入字段,它将提交表单并导致页面刷新,并且请求被取消。