在页面重定向之前将数据发布到服务器异步 [英] Posting data to server async just before page redirect
问题描述
我们正在使用某种点击跟踪器到我们的网站。
此跟踪器在用户点击链接时接到呼叫,我们希望将某些数据填充到不同的服务器,并让用户继续点击链接。
因为我不想在这里等待服务器响应 - 我不等待对这个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
永远不会到达。
考虑使用 你可以在这里加倍你的方法,并保留第二个 这样, 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.
Remove the second Consider using You can double-up your approach here, and keep the second This way, the 这篇关于在页面重定向之前将数据发布到服务器异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! setTimeout
和 成功:
$。ajax()
call!
top.window.location.href
调用,但将它放在 setTimeout
块中,以防出现 $。ajax()
延迟。
$。ajax()
调用应该首先发生,但如果没有,至少用户不会在延迟 $。ajax()
调用的服务器故障时发生延迟。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;
}
top.window.location.href = url;
... that is clobbering your $.ajax()
call... and return false
is never reached.
setTimeout
and success:
on the $.ajax()
call!top.window.location.href
call, but put it in a setTimeout
block, in case there is an $.ajax()
delay.$.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.