AJAX帖子被重定向取消 [英] AJAX Post Gets Canceled By Redirect

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

问题描述

我正在编写一个小脚本来捕获链接点击并将链接的URL保存到数据库表中,以便跟踪单击特定页面上每个链接的次数。链接到外部站点。

I'm writing a small script to capture link clicks and save the link's URL into a database table in order to track how many times each link on a particular page is clicked. The links are to external sites.

因此,我在我的JavaScript函数中捕获click事件,使用jQuery发布到保存MySQL中的数据的PHP页面,以及然后JavaScript函数将用户重定向到他们点击的链接的URL。

So, I capture the click event in my JavaScript function, use jQuery to post to a PHP page that saves the data in MySQL, and then the JavaScript function redirects the user to the URL of the link they clicked on.

我遇到的问题是,帖子似乎从未完成,因为重定向。我通过调用post回调中的重定向来解决这个问题,但是这会增加几秒延迟,因为直到post完成后才会调用它。我正在寻找一种方法来发布数据并立即重定向用户,无论帖子是成功还是失败。

The problem I'm running into is that it seems like the post never completes because the of redirect. I've worked around this by calling the redirect inside the post callback, but that adds a few second delay because it doesn't get called until after the post completes. I'm looking for a way to just post the data and redirect the user immediately, regardless of whether or not the post succeeds or fails.

这是当前的代码解决方法。它工作正常,但增加了延迟:

This is the current code with the workaround. It works fine, but adds the delay:

function trackClicks(event)
{
    var clicked = $(this).attr('href');

    $.post
    (
        $('#track-click-post-url').attr('value'),
        {
            action: 'track_landing_page_clicks',
            clicked_url: clicked,
            nonce: $('#track-click-nonce').attr('value')
        },
        function( response )
        {
            window.location = clicked;
        }
    );

    event.preventDefault(); 
}

这就是我想做的,但是当我尝试它时永远不会完成:

And this is what I'd like to do, but when I try it never completes:

function trackClicks(event)
{
    var clicked = $(this).attr('href');

    $.post
    (
        $('#track-click-post-url').attr('value'),
        {
            action: 'track_landing_page_clicks',
            clicked_url: clicked,
            nonce: $('#track-click-nonce').attr('value')
        }
    );

    window.location = clicked;
    event.preventDefault(); 
}


推荐答案

jQuery不提供回调你正在寻找什么。以下是可用的就绪状态:

jQuery doesn't provide a callback for what you're looking for. Here are the available ready states:

Value   State   Description
0   UNSENT  open()has not been called yet.
1   OPENED  send()has not been called yet.
2   HEADERS_RECEIVED    send() has been called, and headers and status are available.
3   LOADING Downloading; responseText holds partial data.
4   DONE    The operation is complete.

您正在寻找readystate 2,因为这是您最早意识到的事实服务器收到了消息。

You're looking for readystate 2, as that's the earliest you're aware of the fact that the server received the message.

这应该让你开始:

var xhr = new XMLHttpRequest();
xhr.open("POST", clicked);
xhr.onreadystatechange = function() {
    if (xhr.readyState >= 2) window.location = clicked;
};
xhr.send($('#track-click-post-url').attr('value'));

https ://developer.mozilla.org/en/XMLHttpRequest 供进一步阅读。

这篇关于AJAX帖子被重定向取消的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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