在页面重定向之前将数据发布到服务器异步 [英] Posting data to server async just before page redirect

查看:86
本文介绍了在页面重定向之前将数据发布到服务器异步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是一个普遍的问题,但我找不到相关的主题 - 请直接告诉我,如果我错过了这里。



我们正在使用某种点击跟踪器到我们的网站。
此跟踪器在用户点击链接时接到呼叫,我们希望将某些数据填充到不同的服务器,并让用户继续点击链接。



因为我不想在这里等待服务器响应 - 我不等待对这个ajax调用的响应。
但发生的是我失去了这个呼叫,它永远不会到达服务器。
执行此操作的正确方法是什么?



我正在使用top.window.location.href = url;因为我有时在iframe中打开。

  function onClick(url,clickedFrom){
var testClick = ServerHost + 点击
var dataObject = buildCommonData();

$ .ajax({
dataType:'post',
contentType:text / json; charset = utf-8,
data:dataObject,
url:testClick,
async:true,
success:function(response){
top.window.location.href = url;

},

});
top.window.location.href = url;

返回false;


解决方案

删除第二个 top.window.location.href = url; ...这是打破你的 $。ajax()调用...和 return false 永远不会到达。






考虑使用 setTimeout 成功: $。ajax() call!

你可以在这里加倍你的方法,并保留第二个 top.window.location.href 调用,但将它放在 setTimeout 块中,以防出现 $。ajax()延迟。



这样, $。ajax()调用应该首先发生,但如果没有,至少用户不会在延迟 $。ajax()调用的服务器故障时发生延迟。


It seems like a common problem, but I couldn't find related threads - please direct me if I missed something here.

we are using some sort of click tracker to our website. this tracker is getting calls when the user click on a link, we want to populate certain data to a different server, and let the user continue to the clicked url.

As I do not want to wait for the server response here- I am not waiting for response to this ajax call. but what happens is the I lose this call and it never gets to the server. What is the correct way to perfrom this action?

I am using top.window.location.href = url; as I sometimes get opened in an Iframe.

function onClick(url, clickedFrom) {
        var testClick = ServerHost + "click";
        var dataObject = buildCommonData();

        $.ajax({
            dataType: 'post',
            contentType: "text/json; charset=utf-8",
            data: dataObject,
            url: testClick,
            async: true,
            success: function (response) {
                top.window.location.href = url;

            },

        });
        top.window.location.href = url;

        return false;
    }

解决方案

Remove the second top.window.location.href = url; ... that is clobbering your $.ajax() call... and return false is never reached.


Consider using setTimeout and success: on the $.ajax() call!

You can double-up your approach here, and keep the second top.window.location.href call, but put it in a setTimeout block, in case there is an $.ajax() delay.

This way, the $.ajax() call should happen first, but if not, at least the user is not delayed if/when there is a server glitch that hangs up the $.ajax() call.

这篇关于在页面重定向之前将数据发布到服务器异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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