简体   繁体   English

jquery:如何等待ajax调用完成

[英]jquery: how to wait until ajax call would be completed

I have an anchor on my page that leads to some other website. 我的页面上有一个锚点,可以访问其他网站。

I want to keep track on clicking that anchor, so I wrote a little click handler. 我想跟踪点击那个锚,所以我写了一个点击处理程序。

<a href="https://othersite.com" class="js-button">Leave</a>

and there's javascript code... 还有javascript代码......

$(".js-button").on("click", function() {
    var name = $(this).data("name");
    $.when(
        $.ajax({
            url : 'statscript.php',
            type : 'post',
            data : 'name=' + name,
            success : function(response) {                  
            }
        })
    ).then(); 
});

Though it should wait for an ajax to load and only after that leave the page, in fact it doesn't work. 虽然它应该等待ajax加载,但只有在离开页面之后,实际上它不起作用。 The statscript.php doesn't even start working, and user leaves the page. statscript.php甚至没有开始工作,用户离开页面。

So if I alter the string with then to 所以,如果我改变字符串然后到

).then(alert('hey'); 

the php script works fine because it looks like it has time to work. PHP脚本工作正常,因为它看起来有时间工作。

So what am I doing wrong with the wait function? 那么我在wait函数上做错了什么?

You were close to the answer: There is a done function called like this: 你接近答案:有一个像这样的完成函数:

$.ajax({
  url: "http://fiddle.jshell.net/favicon.png",
  beforeSend: function( xhr ) {
      xhr.overrideMimeType( "text/plain; charset=x-user-defined" );
  }
}).done(function( data ) {
    if ( console && console.log ) {
        console.log( "Sample of data:", data.slice( 0, 100 ) );
    }
});

The closest solution to your code would be: 最接近您的代码的解决方案是:

You should use e.preventDefault() to prevent the default action of the a tag. 您应该使用e.preventDefault()来阻止a标记的默认操作。

You should also redirect the user on complete . 您还应该在完成时重定向用户。 if you got error in your analytics system, he doesn't need to know that. 如果您的分析系统出错,他不需要知道。

You should do it like that: 你应该这样做:

$(".js-button").on("click", function(e) {
    e.preventDefault();

    var name = $(this).data("name"),
        href = $(this).attr("href");

    $.ajax({
        url : 'statscript.php',
        type : 'post',
        data : 'name=' + name,
        complete: function () {
            alert('Hey!');
            // if you want to redirect the user after the statistics sent:
            //window.location.href = href;
        }
    })
});

I think you have two problems. 我认为你有两个问题。 One Jnn Mgdls responded to - the AJAX done callback on the promise is to be used. 一位Jnn Mgdls回应 - AJAX完成了对诺言的回调。

The second problem may be related to the anchor's event causing a navigation away from the page, and you may need to prevent the default behavior - http://api.jquery.com/event.preventdefault/ 第二个问题可能与导致离开页面的导航事件有关,您可能需要阻止默认行为 - http://api.jquery.com/event.preventdefault/

You need to disable the default browser action and then manually redirect the user: 您需要禁用默认浏览器操作,然后手动重定向用户:

$(".js-button").on("click", function(e) {
    e.preventDefault();
    var name = $(this).data("name"),
        href = this.href;
    $.ajax({
        url : 'statscript.php',
        type : 'post',
        data : 'name=' + name,
        success : function(response) {
            document.location.href = href;
        }
    }); 
});

There is a function called ajaxComplete() 有一个名为ajaxComplete()的函数

$(document).ajaxComplete(function() {
  alert('Ajax done');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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